一、概述
外观模式(Facade),也叫过程模式,门面模式,属于结构型模式,外观模式为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
外观模式通过定义一个一致的接口,用于屏蔽内部子系统的细节,使得调用端只需跟这个接口发生调用,而无需关心这个子系统的内部细节。
二、角色
外观角色(Facade): 客户端通过操作外观角色从而达到控制子系统角色的目的,对于客户端来说,外观角色好比一道屏障,对客户端屏蔽了子系统的具体实现
子系统角色(SubSystem):表示一个系统的子系统或模块
三、外观模式
1.子系统角色
class CPU {
public void on() {
System.out.println("CPU on...");
}
public void off() {
System.out.println("CPU off...");
}
}
class Disk {
public void on() {
System.out.println("Disk on...");
}
public void off() {
System.out.println("Disk off...");
}
}
class Memory {
public void on() {
System.out.println("Memory on...");
}
public void off() {
System.out.println("Memory off...");
}
}
2.外观角色
public class ComputerFacade {
private CPU cpu;
private Memory memory;
private Disk disk;
public ComputerFacade() {
cpu = new CPU();
memory = new Memory();
disk = new Disk();
}
public void on() {
cpu.on();
memory.on();
disk.on();
}
public void off() {
cpu.off();
memory.off();
disk.off();
}
}
3.使用
@Test
public void testFacade() {
ComputerFacade computer = new ComputerFacade();
computer.on();
computer.off();
}