Facade Pattern hides the complexity of the system and provides the client with an interface through which the client can access the system. This type of design pattern belongs to structural pattern, which adds an interface to the existing system to hide the complexity of the system.
This pattern involves a single class that provides simplified methods for client requests and delegate calls to existing system class methods. 意图: Provides a consistent interface for a set of interfaces in the subsystem, and the appearance pattern defines a high-level interface that makes the subsystem easier to use. 主要解决: Reduce the complexity of accessing the internal subsystems of complex systems and simplify the interface between clients. 何时使用: 1. The client does not need to know the complex connections within the system, the whole system only needs to provide a “receptionist”. 2. Define the entrance of the system. 如何解决: The client is not coupled to the system, and the appearance class is coupled to the system. 关键代码: Add another layer between the client and the complex system, which deals with the order of calls, dependencies, and so on. 应用实例: 1. When you go to the hospital to see a doctor, you may have to register, outpatient, price, and take medicine, so as to make the patients or their families feel very complicated. If there is a receptionist, it is very convenient to let the receptionist handle it only. 2. The three-tier development mode of JAVA. 优点: 1. Reduce the interdependence of systems. 2. Improve flexibility. 3. The security is improved. 缺点: Do not comply with the principle of opening and closing, if it is troublesome to change things, it is not appropriate to inherit and rewrite. 使用场景: 1. A module that provides external access for complex modules or subsystems. 2. The subsystems are relatively independent. 3. Prevent the risks brought by low-level personnel. 注意事项: In a hierarchical structure, you can use appearance patterns to define entrances to each layer of the system. We will create a Shape Interface and implementation of the Shape The entity class of the interface. The next step is to define an appearance class ShapeMaker . ShapeMaker Classes use entity classes to represent user calls to these classes. FacadePatternDemo Class usage ShapeMaker Class to display the results. Create an interface. Create an entity class that implements the interface. Create an appearance class. Use this appearance class to draw various types of shapes. Execute the program and output the result: 6.13.1. Introduction ¶
6.13.2. Realize ¶
6.13.3. Step 1 ¶
Shape.java ¶
publicinterfaceShape{voiddraw();}
6.13.4. Step 2 ¶
Rectangle.java ¶
publicclassRectangleimplementsShape{@Overridepublicvoiddraw(){System.out.println("Rectangle::draw()");}}
Square.java ¶
publicclassSquareimplementsShape{@Overridepublicvoiddraw(){System.out.println("Square::draw()");}}
Circle.java ¶
publicclassCircleimplementsShape{@Overridepublicvoiddraw(){System.out.println("Circle::draw()");}}
6.13.5. Step 3 ¶
ShapeMaker.java ¶
publicclassShapeMaker{privateShapecircle;privateShaperectangle;privateShapesquare;publicShapeMaker(){circle=newCircle();rectangle=newRectangle();square=newSquare();}publicvoiddrawCircle(){circle.draw();}publicvoiddrawRectangle(){rectangle.draw();}publicvoiddrawSquare(){square.draw();}}
6.13.6. Step 4 ¶
FacadePatternDemo.java ¶
publicclassFacadePatternDemo{publicstaticvoidmain(String[]args){ShapeMakershapeMaker=newShapeMaker();shapeMaker.drawCircle();shapeMaker.drawRectangle();shapeMaker.drawSquare();}}
6.13.7. Step 5 ¶
Circle::draw()
Rectangle::draw()
Square::draw()