Facade Pattern, also known as appearance mode, hides the complexity of the system and provides the client with an interface that can access the system. This type of design pattern is a structural pattern. It adds an interface to the existing system to hide the complexity of the system and provides a unified high-level access interface for a set of interfaces in the subsystem, which makes the subsystem easier to access or use. This pattern involves a single class that provides simplified methods for client requests and delegated calls to existing system class methods.
In short, it is to encapsulate a bunch of complex processes into an interface for users to use for easier use. There are three roles in this design pattern:
1) Facade character (facade): This is the core of the facade mode. It is called by the client role, so it is familiar with the functionality of the subsystem. It internally reserves several functional combinations based on the existing needs of the customer role.
2) Subsystem role (subsystem): implements the functions of the subsystem. For it, the façade role is as unknown as the client role, and it does not have any information and links to the façade role.
3) Client role: Call the façade role to complete the functions to be obtained.
Here is a simple implementation example:
// Define a unified entrance 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();}}// Directly use the appearance class defined previously to draw various shapes. As for how to obtain and draw, there is no need to pay attention to public class FacadePatternDemo {public static void main(String[] args) {ShapeMaker shapeMaker = new ShapeMaker();shapeMaker.drawCircle();shapeMaker.drawRectangle();shapeMaker.drawSquare();}}Figure 1: UML diagram
class DrawerOne {public void open() {System.out.println("The first drawer is opened");getKey();}public void getKey() {System.out.println("Get the key to the second drawer");}}class DrawerTwo {public void open() {System.out.println("The second drawer is opened");getFile();}public void getFile() {System.out.println("Get this important file");}}class DrawerFacade {DrawerOne darwerOne = new DrawerOne();DrawerTwo darwerTwo = new DrawerTwo();public void open() {darwerOne.open();darwerTwo.open();}}public class DrawerClient {public static void main(String[] args) {DrawerFacade drawer = new DrawerFacade();drawer.open();}}Facade mode usage scenario:
(1) Modules that provide external access to complex modules or subsystems.
(2) The subsystem is relatively independent.
(3) In the hierarchical structure, the entrance of each layer in the system can be defined using the appearance pattern.
The above is the detailed explanation of the Java design model (appearance mode) introduced by the editor. I hope it will be helpful to everyone!