class CPU {
    void start() {
        System.out.println("CPU: starting...");
    }
    void shutdown() {
        System.out.println("CPU: shutting down...");
    }
}

class Memory {
    void load() {
        System.out.println("Memory: loading data...");
    }
    void free() {
        System.out.println("Memory: freeing data...");
    }
}

class HardDrive {
    void read() {
        System.out.println("HDD: reading data...");
    }
    void write() {
        System.out.println("HDD: writing data...");
    }
}

class ComputerFacade {
    private CPU cpu;
    private Memory memory;
    private HardDrive hdd;

    ComputerFacade() {
        this.cpu = new CPU();
        this.memory = new Memory();
        this.hdd = new HardDrive();
    }

    void start() {
        System.out.println("=== Starting Computer ===");
        cpu.start();
        memory.load();
        hdd.read();
        System.out.println("=== Computer Ready ===");
    }

    void shutdown() {
        System.out.println("=== Shutting Down ===");
        hdd.write();
        memory.free();
        cpu.shutdown();
        System.out.println("=== Goodbye ===");
    }
}

public static void main(String[] args) {
    ComputerFacade computer = new ComputerFacade();
    computer.start();
    computer.shutdown();
}
Back to Design Patterns
Structural Design Pattern

Facade

The Facade pattern provides a simplified interface to a complex subsystem. It defines a higher-level interface that makes the subsystem easier to use by reducing dependencies and hiding the complexity of the underlying components. Clients interact with the facade instead of dealing with multiple subsystem classes directly.