门面模式(Facade)又叫外观模式
1,使用场景
为子系统中的一组接口提供一个一致的界面, Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
2,特点
1,利用高内聚来降耦合。
2,通过门面角色来控制子系统角色的方法调用,
3,UML类图
1,子系统角色:Disk,Memory,CPU
2,门面角色:Computer
3,客户端:FacadeTest
public class CPU {
void startup(){
System.out.println("cpu startup");
}
void shutdown(){
System.out.println("cup shutdown");
}
}
public class Disk {
void startup(){
System.out.println("disk startup");
}
void shutdown(){
System.out.println("disk shutdown");
}
}
public class Memory {
void startup(){
System.out.println("memory startup");
}
void shutdown(){
System.out.println("memory shutdown");
}
}
public class Computer {
private CPU cpu;
private Memory memory;
private Disk disk;
public Computer(){
this.cpu = new CPU();
this.memory = new Memory();
this.disk = new Disk();
}
void startup(){
this.cpu.startup();
this.disk.startup();
this.memory.startup();
}
void shutdown(){
this.memory.shutdown();
this.disk.shutdown();
this.cpu.shutdown();
}
}
public class FacadeTest {
public static void main(String[] args) {
Computer computer = new Computer();
computer.startup();
computer.shutdown();
}
}