Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Appearance mode


May 27, 2021 Design mode


Table of contents


Appearance mode

Facade Pattern hides the complexity of the system and provides the client with an interface that the client can access the system. This type of design pattern belongs to 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 a simplified method for client requests and delegate calls to existing system class methods.

Introduced

Intent: Provides a consistent interface for a set of interfaces in a subsyscies, and the skin pattern defines a high-level interface that makes this subsyscies easier to use.

Key solution: Reduce the complexity of accessing the internal subsyscies of complex systems and simplify the interface between clients and them.

When to use: 1, the client does not need to know the complex connections within the system, the entire system only need to provide a "receptionist" can be. 2 , define the entrance of the system.

How to solve: The client is not coupled with the system, the appearance class is coupled with the system.

Key code: Add another layer between the client and the complex system, this time handling the call order, dependencies, and so on.

Application example: 1, go to the hospital to see a doctor, may have to register, outpatient, price, medicine, so that patients or patients' families feel very complex, if there is a reception staff, only let the reception staff to deal with, it is very convenient. 2 , JAVA's three-tier development model.

Pros: 1, reduce system interdependence. 2 , improve flexibility. 3 , improve security.

Cons: does not conform to the open and closed principle, if you want to change things is very troublesome, inheritance rewriting is not appropriate.

Use Scenarios: 1, modules that provide outside access to complex modules or subsysyssies. 2 , subsysysses are relatively independent. 3 , to prevent the risk of low-level personnel.

Note: In a hierarchical structure, you can use skin patterns to define the entry point to each layer of the system.

Realize

We'll create a Shape interface and an entity class that implements the Shape interface. The next step is to define a skin class ShapeMaker.

ShapeMaker classes use entity classes to represent user calls to those classes. FacadePatternDemo, our demo class uses the ShapeMaker class to display the results.

Appearance mode

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 skin 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 skin 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()