This is the 11th day of my participation in the August Wenwen Challenge.More challenges in August

Facade patterns are often used to hide the complexity of a system and provide an interface through which clients can access the system. This type of design pattern is structural in that it adds an interface to an 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.

introduce

describe parsing
intentions To provide a consistent interface for a set of interfaces in a subsystem, the facade defines a high-level interface that makes the subsystem easier to use
Mainly to solve Reduce the complexity of accessing the internal subsystems of complex systems and simplify the interfaces between clients.
When to use 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 entry to the system.
How to solve The client is not coupled to the system; the facade class is.
The key code Add another layer between the client and the complex system, which handles the order of calls, dependencies, and so on.
Examples of application 1, go to the hospital to see a doctor, may have to go to registration, outpatient service, pricing, medicine, so that patients or patients’ families feel very complicated, if there is a reception staff, only let the reception staff to deal with, it is very convenient. 2. JAVA three-tier development mode.
advantages Inconsistent open and close principle, if you want to change things is very troublesome, inheritance rewrite are not appropriate.
disadvantages Multi-layer decoration is more complicated.
Usage scenarios Modules that provide external access to complex modules or subsystems. 2. Subsystems are relatively independent. 3. Prevent risks brought by low-level personnel.
Matters needing attention In a hierarchical structure, facade patterns can be used to define entry points for each layer in the system.

implementation

Create a Shape interface and an entity class that implements Shape. The next step is to define a appearance class ShapeMaker.

The ShapeMaker class uses entity classes to represent user calls to these classes. The FacadePatternDemo class uses the ShapeMaker class to display the results.

Step 1 – Create a graphical interface

Create an interface.

// Shape.java
public interface Shape {
   void draw();
}
Copy the code

Step 2 – Create the entity class based on the interface from step 1

Create an entity class that implements the interface.

// Rectangle.java public class Rectangle implements Shape { @Override public void draw() { System.out.println("Rectangle::draw()"); }}Copy the code
// Square.java public class Square implements Shape { @Override public void draw() { System.out.println("Square::draw()"); }}Copy the code
// Circle.java public class Circle implements Shape { @Override public void draw() { System.out.println("Circle::draw()"); }}Copy the code

Step 3 – Create the appearance class

Create a facade class.

// ShapeMaker.java public class ShapeMaker { private Shape circle; private Shape rectangle; private Shape square; Public ShapeMaker() {// Strong coupling with entity class circle = new circle (); rectangle = new Rectangle(); square = new Square(); } public void drawCircle(){ circle.draw(); } public void drawRectangle(){ rectangle.draw(); } public void drawSquare(){ square.draw(); }}Copy the code

Step 4 – Define the scene class

Use this appearance class to draw various types of shapes.

// FacadePatternDemo.java public class FacadePatternDemo { public static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); shapeMaker.drawCircle(); shapeMaker.drawRectangle(); shapeMaker.drawSquare(); }}Copy the code

Step 5 – Output results

Circle::draw()
Rectangle::draw()
Square::draw()
Copy the code