Interview asked about appearance mode? ? ? Isn't that what design patterns are all about? Let me teach you a lesson, interviewer

Interview asked about appearance mode? ? ? Isn't that what design patterns are all about? Let me teach you a lesson, interviewer

Appearance Mode

The 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.

introduce

Intended to provide a consistent interface for a set of interfaces in a subsystem, the Facade pattern defines a high-level interface that makes the subsystem easier to use.

The main solution is to reduce the complexity of accessing the internal subsystems of complex systems and simplify the interface between the client and it.

When to use 1. The client does not need to know the complex connections within the system, and the entire system only needs to provide a "receptionist". 2. Define the entrance of the system.

How to solve the problem that the client is not coupled with the system, and the appearance class is coupled with the system.

The key code adds another layer between the client and the complex system, and this layer handles the order of calls, dependencies, etc.

Application example 1. Going to the hospital to see a doctor may involve registration, outpatient treatment, price adjustment, and medicine collection, which makes the patient or the patient's family feel very complicated. If there is a receptionist, only the receptionist can handle it, which is very convenient. 2. The three-tier development model of JAVA.

Advantages 1. Reduce system interdependence. 2. Improve flexibility. 3. Improve the security.

The disadvantage is that it does not conform to the open-closed principle. If it is very troublesome to change something, inheritance and rewriting are not suitable.

Use Scenario 1. Modules that provide external access to complex modules or subsystems. 2. The subsystems are relatively independent. 3. Prevent the risks brought by low-level personnel.

Considerations In a hierarchical structure, you can use the Appearance pattern to define the entry for each level in the system.

accomplish

We will create a Shape interface and an entity class that implements the Shape interface. The next step is to define a skin 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 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 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

Execute the program and output the result:

Circle::draw()
Rectangle::draw()
Square::draw()

Collecting is equal to whoring for nothing, three consecutive times is the truth (funny)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324146159&siteId=291194637