Design patterns
patterns10FacadePattern
- Path
- pkg8patterns/patterns10FacadePattern.java
- Package
- pkg8patterns
- Study order
- 10
- Run
- Single-file source launch
- Command
- java pkg8patterns/patterns10FacadePattern.java
There is no in-browser runner. This is the file from the curriculum, unchanged.
1package pkg8patterns;2 3/*4 * Facade (Structural)5 * -------------------6 * INTENT: provide a single, simplified interface to a complex subsystem.7 * UML: Facade --> {SubsystemA, SubsystemB, SubsystemC}.8 * PROS: reduces coupling; easier to use; hides complexity.9 * CONS: can become a god object if it grows unchecked.10 * REAL-WORLD: javax.faces, JOptionPane, a "service" wrapping repositories.11 */12public class patterns10FacadePattern {13 14 // Complex subsystem15 static class Cpu { String freeze() { return "CPU freeze"; } String execute() { return "CPU execute"; } }16 static class Memory { String load(String data) { return "Memory load(" + data + ")"; } }17 static class HardDrive { String read() { return "HardDrive read boot sector"; } }18 19 // Facade exposes a simple operation20 static class Computer {21 private final Cpu cpu = new Cpu();22 private final Memory memory = new Memory();23 private final HardDrive disk = new HardDrive();24 void start() {25 System.out.println(cpu.freeze());26 System.out.println(memory.load(disk.read()));27 System.out.println(cpu.execute());28 System.out.println("Computer started.");29 }30 }31 32 public static void main(String[] args) {33 new Computer().start(); // client uses one simple call34 }35}