使用对外的统一接口,供外部系统访问的设计模式

外观模式的核心内部和外部方法的解耦。在外部模块和内部模块中创建一个“中间人”,对外部模块暴露另一套不同的方法,外部仅需要直接调用暴露的方法即可,不需要关心内部的细节。

src: 外观模式 | 菜鸟教程

  • ShapeMaker 类实现 drawCircle(),drawRectangle(),drawSquare() 方法均是供外部模块使用,而内部系统中则有完全不同的另一套结构。
public interface Shape {
   void draw();
}
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();
   }
}