appearance mode
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 is a structural pattern that 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 delegated calls to existing system class methods.
Introduction
Intent:Provide a consistent interface for a set of interfaces in a subsystem. The appearance pattern defines a high-level interface that makes this subsystem Easier to use.
Main solutions: Reduce the complexity when accessing the internal subsystems of complex systems and simplify the client's interface with it.
When to use: 1. The client does not need to know the complex connections within the system. The entire system only needs to provide a "receptionist". 2. Define the entrance to the system.
How to solve: The client is not coupled with the system, and the appearance class is coupled with the system.
Key code: Add another layer between the client and the complex system, this time the calling sequence, dependencies, etc. will be processed.
Application examples: 1. When you go to the hospital to see a doctor, you may have to register, go to the outpatient clinic, negotiate prices, and get medicine, which makes the patient or the patient's family feel very complicated. If there is a receptionist, only let It is very convenient to have the reception staff handle it. 2. JAVA’s three-tier development model.
Advantages: 1. Reduce system interdependence. 2. Improve flexibility. 3. Improved safety.
Disadvantages: Does not comply with the opening and closing principle. If it is troublesome to change things, inheritance and rewriting are not suitable.
Usage scenarios: 1. Modules that provide external access to complex modules or subsystems. 2. Subsystems are relatively independent. 3. Prevent risks caused by low-level personnel.
Note: In a hierarchical structure, you can use appearance mode to define the entrance to each layer in the system.
Implementation
We will create a Shape interface and an entity class that implements the Shape interface. The next step is to define a appearance class ShapeMaker.
ShapeMaker classes use entity classes to represent user calls to these classes. FacadePatternDemo, our demo class uses the ShapeMaker class to display the results.
Step 1
Create an interface.
Shape.java
public interface Shape { void draw(); }
Step 2
Create an entity class that implements the interface.
Rectangle.java
public class Rectangle implements Shape { @Override public void draw() { System.out.println("Rectangle::draw()"); } }
Square.java
public class Square implements Shape { @Override public void draw() { System.out.println("Square::draw()"); } }
Circle.java
public class Circle implements Shape { @Override public void draw() { System.out.println("Circle::draw()"); } }
Step 3
Create a appearance class.
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(); } }
Step 4
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(); } }
Step 5
Verify the output.
Circle::draw() Rectangle::draw() Square::draw()