public class Facade {
public static void main(String[] args) {
Computer com = new Computer();
com.startup();
com.shutdown();
}
}
class Computer {
private CPU cpu;
private Memory memory;
private Disk disk;
public Computer(){
cpu = new CPU();
memory = new Memory();
disk = new Disk();
}
public void startup(){
System.out.println("start the computer!");
cpu.startup();
memory.startup();
disk.startup();
System.out.println("start computer finished!");
}
public void shutdown(){
System.out.println("begin to close the computer!");
disk.shutdown();
memory.shutdown();
cpu.shutdown();
System.out.println("computer closed!");
}
}
class CPU {
public void startup(){
System.out.println("cpu startup!");
}
public void shutdown(){
System.out.println("cpu shutdown!");
}
}
class Memory {
public void startup(){
System.out.println("memory startup!");
}
public void shutdown(){
System.out.println("memory shutdown!");
}
}
class Disk {
public void startup(){
System.out.println("disk startup!");
}
public void shutdown(){
System.out.println("disk shutdown!");
}
}
输出为:
start the computer!
cpu startup!
memory startup!
disk startup!
start computer finished!
begin to close the computer!
disk shutdown!
memory shutdown!
cpu shutdown!
computer closed!
* 为一个复杂子系统提供一个简单接口时,由于子系统往往因为不断演化而变得越来越复杂,但这种变化
不应该影响到客户的调用,此时使用 Facade 模式对外提供一个访问的接口。
* Facade模式注重简化接口,Adapter模式注重转换接口,Bridge模式注重分离接口(抽象)与其实现,
Decorator模式注重稳定接口的前提下为对象扩展功能。