:::info Facade Pattern 有时也翻译成面板模式,是一个使用频率极高的设计模式。 :::
定义
外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。
这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。
使用场景
降低访问复杂系统的内部子系统时的复杂度,简化客户端与之的接口。
- 客户端不需要知道系统内部的复杂联系,整个系统只需提供一个”接待员”即可。
 - 
UML
角色结构
 Facade:就这个一个关键部分,里面引用各个子模块,然后对外提供统一的接口
- 
优点
 减少系统相互依赖。
- 提高灵活性。
 - 
缺点
代码示例
Shape.java
public interface Shape {void draw();}
Rectangle.java
public class Rectangle implements Shape {@Overridepublic void draw() {System.out.println("Rectangle::draw()");}}
Square.java
public class Square implements Shape {@Overridepublic void draw() {System.out.println("Square::draw()");}}
Circle.java
public class Circle implements Shape {@Overridepublic void draw() {System.out.println("Circle::draw()");}}
外观类ShapeMaker.java
public class ShapeMaker {private Shape circle;private Shape rectangle;private Shape square;public ShapeMaker() {circle = new Circle();rectangle = new Rectangle();square = new Square();}public void drawCircle(){circle.draw();}public void drawRectangle(){rectangle.draw();}public void drawSquare(){square.draw();}}
Test.java
public class Test {public static void main(String[] args) {ShapeMaker shapeMaker = new ShapeMaker();shapeMaker.drawCircle();shapeMaker.drawRectangle();shapeMaker.drawSquare();}}
输出
Circle::draw()Rectangle::draw()Square::draw()
源码示例
 
