设计模式:外观模式
设计模式:外观模式
外观设计模式(Facade design pattern)隐藏了任务内部错综复杂的处理而为我们提供了简单的功能接口。电脑的启动是一个很好的例子。电脑的启动包含CPU启动,内存启动,硬盘启动等等。为了能让用户简便使用,我们添加一个外观,仅仅提供一个简单的接口,来隐藏电脑内部繁琐或者负责的内部处理。
1、外观模式的类图
2、外观模式的例子
//电脑的部件
class CPU {
public void processData() { }
}
class Memory {
public void load() { }
}
class HardDrive {
public void readdata() { }
}
/* 外观 */
class Computer {
private CPU cpu;
private Memory memory;
private HardDrive hardDrive;
public Computer() {
this.cpu = new CPU();
this.memory = new Memory();
this.hardDrive = new HardDrive();
}
public void run() {
cpu.processData();
memory.load();
hardDrive.readdata();
}
}
class User {
public static void main(String[] args) {
Computer computer = new Computer();
computer.run();
}
}