外观模式

2019-05-09  本文已影响0人  蜗牛ICU

前言:

因为现在设计模式在网络上已经泛滥,但是还是有好多程序员不能够灵活的运用设计模式,这个是对设计模式简单的介绍,因为网络上比较多类似的文章,所以本人就从网络上抄了一部分,等23种设计模式整理完成之后会根据实际的需求利用设计模式在代码中设计一些开源的插件,请继续关注。
原版[菜鸟教程]

外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。
意图:
主要解决:
何时使用:
如何解决:
关键代码:
应用实例:
优点:
缺点:
使用场景:
第一步:
public interface Shape {
   void draw();
}
第二步:
public class Rectangle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Rectangle::draw()");
   }
}


public class Square implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Square::draw()");
   }
}

public class Circle implements Shape {
 
   @Override
   public void draw() {
      System.out.println("Circle::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();
   }
}

第四步:
public class FacadePatternDemo {
   public static void main(String[] args) {
      ShapeMaker shapeMaker = new ShapeMaker();
 
      shapeMaker.drawCircle();
      shapeMaker.drawRectangle();
      shapeMaker.drawSquare();      
   }
}

第五步:
Circle::draw()
Rectangle::draw()
Square::draw()

上一篇 下一篇

猜你喜欢

热点阅读