外观模式给多个复杂子系统提供一个统一的界面,使得用户对子系统的使用更简便。
用户故事:家庭影院中有一套智能设备,每个设备都有若干个操作方法,用户希望通过一个手机APP经过简单的几个按键就对所有设备执行启动、关闭等操作。
一、UML类图
二、代码示例
三个具体类
1. DVD
package behaviour.facade;
public class DVDPlayer {
public static DVDPlayer getInstance() {
return Holder.player;
}
private DVDPlayer() {}
private static class Holder {
private static DVDPlayer player = new DVDPlayer();
}
public void on() {
System.out.println("DVD on");
}
public void off() {
System.out.println("DVD off");
}
public void play() {
System.out.println("DVD playing");
}
public void pause() {
System.out.println("DVD paused");
}
}
2. Projector
package behaviour.facade;
public class Projector {
public static Projector getInstance() {
return Holder.projector;
}
private Projector() {}
private static class Holder {
private static Projector projector = new Projector();
}
public void on() {
System.out.println("Projector on");
}
public void off() {
System.out.println("Projector off");
}
public void focus() {
System.out.println("Popcorn is focusing");
}
}
3. Popcorn Machine
package behaviour.facade;
public class Popcorn {
public static Popcorn getInstance() {
return Holder.popMachine;
}
private Popcorn() {}
private static class Holder {
private static Popcorn popMachine = new Popcorn();
}
public void on() {
System.out.println("Popcorn on");
}
public void off() {
System.out.println("Popcorn off");
}
public void pop() {
System.out.println("Popcorn making popcorn");
}
}
外观类
package behaviour.facade;
public class ApplianceFacade {
private DVDPlayer player;
private Popcorn popMachine;
private Projector projector;
public ApplianceFacade() {
this.player = DVDPlayer.getInstance();
this.popMachine = Popcorn.getInstance();
this.projector = Projector.getInstance();
}
public void on() {
player.on();
popMachine.on();
projector.on();
}
public void play() {
player.play();
}
public void pop() {
popMachine.pop();
}
public void off() {
player.off();
popMachine.off();
projector.off();
}
}
客户类
package behaviour.facade;
public class Client {
public static void main(String[] args) {
ApplianceFacade facade = new ApplianceFacade();
facade.on();
facade.pop();
facade.play();
facade.off();
}
}
运行结果
DVD on
Popcorn on
Projector on
---------------
Popcorn making popcorn
---------------
DVD playing
---------------
DVD off
Popcorn off
Projector off
Process finished with exit code 0
三、参考
- https://www.runoob.com/design-pattern/facade-pattern.html
- https://www.javadevjournal.com/java-design-patterns/facade-design-pattern/
- 韩顺平 《设计模式》
设计模式系列博文导航
一、创建型 - 5种
原型模式(Prototype Pattern)
抽象工厂模式(Abstract Factory Pattern)
建造者模式(Builder Pattern)
工厂模式(Factory Pattern)
单例模式(Singleton Pattern)
助记语:原抽建工单
二、结构型 - 8种
享元模式(Flyweight Pattern)
代理模式(Proxy Pattern)
适配器模式(Adapter Pattern)
外观模式(Facade Pattern)
过滤器模式(Filter/Criteria Pattern)
桥接模式(Bridge Pattern)
组合模式(Composite Pattern)
装饰器模式(Decorator Pattern)
助记语:想呆室外,过桥组装
三、行为型 - 11种
责任链模式(Chain of Responsibility Pattern)
命令模式(Command Pattern)
解释器模式(Interpreter Pattern)
中介者模式(Mediator Pattern)
迭代器模式(Iterator Pattern)
观察者模式(Observer Pattern)
策略模式(Strategy Pattern)
状态模式(State Pattern)
备忘录模式(Memento Pattern)
模板方法模式(Template Pattern)
访问者模式(Visitor Pattern)
助记语:责令解中谍,观测状被模仿