Facade Design Pattern in Java
Facade gives one simple class in front of a complex system. You call one method and it does many steps inside.
Real life example
Pressing the power button on a computer. Many parts start, but you press only one button.
When to use
A system has many classes and you want an easy way to use it.
Complete Java example
Save this as Main.java, then run javac Main.java and java Main.
public class Main {
public static void main(String[] args) {
new Computer().turnOn();
}
}
class Cpu { void start() { System.out.println("CPU started"); } }
class Memory { void load() { System.out.println("Memory loaded"); } }
class Disk { void read() { System.out.println("Disk read"); } }
class Computer { // the facade
private Cpu cpu = new Cpu();
private Memory memory = new Memory();
private Disk disk = new Disk();
void turnOn() { // one easy method
cpu.start();
memory.load();
disk.read();
}
}Output
CPU started
Memory loaded
Disk read