“This is the 24th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Java appearance pattern
The Facade Pattern hides the complexity of the system and provides clients with an interface through which they 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
Intent: 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.
Main solution: reduce the complexity of accessing the internal subsystem of complex system, simplify the interface between the client and it.
When to use:
⒈ The client does not need to know the complicated internal connections of the system. The whole system only needs to provide a “receptionist”.
⒉ Defines the entrance to the system.
How to fix it: The client is not coupled to the system, the facade classes are.
Key code: Add another layer between the client and the complex system, this layer takes care of the order of calls, dependencies, and so on.
Application examples:
⒈ Go to the hospital to see a doctor, may have to go to registration, outpatient service, price, take 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.
⒉JAVA three-tier development mode.
Advantages:
⒈ Reduce system interdependence.
⒉ Improve flexibility.
⒊ Improved safety.
Disadvantages: inconsistent with the principle of open and close, if you want to change things is very troublesome, inheritance rewrite are not appropriate.
Usage Scenarios:
⒈ MODULES that provide external access to complex modules or subsystems.
The ⒉ subsystem is relatively independent.
(c) Prevent the risk of low-level personnel.
Note: In a hierarchical structure, facade patterns can be used to define entry points for 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.
The ShapeMaker class uses 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.
public interface Shape {
void draw();
}
Copy the code
Step 2
Create an entity class that implements the interface.
public class Rectangle implements Shape { @Override public void draw() { System.out.println("Rectangle::draw()"); }}Copy the code
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Square::draw()");
Copy the code
public class Circle implements Shape { @Override public void draw() { System.out.println("Circle::draw()"); }}Copy the code
Step 3
Create a facade class.
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(); }}Copy the code
Step 4
Use this appearance class to draw various types of shapes.
public class FacadePatternDemo { public static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); shapeMaker.drawCircle(); shapeMaker.drawRectangle(); shapeMaker.drawSquare(); }}Copy the code
Step 5
Execute the program and output the result:
Circle::draw()
Rectangle::draw()
Square::draw()
Copy the code