Design Pattern Notes 16 - Visitor Pattern (visitor)

Complete the assessment system requirements

1) Divide the audience into men and women, evaluate the singers, and get their different evaluations of the singer after watching a singer's performance (there are different types of evaluations, such as success, failure, etc.)

 

 

Traditional way of problem analysis

1) If the system is relatively small, it is still ok, but when considering adding more and more new functions to the system, the code changes are relatively large, which violates the ocp principle and is not conducive to maintenance

2) The scalability is not good, such as adding new types of personnel or management methods, it is not easy to do

3) Lead us to use a new design pattern – the visitor pattern

 

Basic introduction to visitor pattern

1) Visitor Pattern (Visitor Pattern), which encapsulates some operations on the elements of a certain data structure, it can define new operations on these elements without changing the data structure.

2) Mainly separate the data structure and data operation to solve the coupling problem of data structure and operation

3) The basic working principle of the visitor mode is: add an interface for receiving visitors in the visited class

4) The main application scenario of the visitor mode is: it is necessary to perform many different operations on objects in an object structure (these operations are not related to each other), and at the same time, it is necessary to avoid these operations from "polluting" the classes of these objects, which can be solved by using the visitor mode

 

Principle class diagram of the visitor pattern

Explanation of the principle class diagram - that is (roles and responsibilities of the visitor mode)

1) Visitor is an abstract visitor, which declares a visit operation for each class of ConcreteElement in the object structure

2) ConcreteVisitor: It is a specific visit value to implement each operation declared by Visitor, and it is a part of each operation implementation.

3) ObjectStructure can enumerate its elements and provide a high-level interface to allow visitors to access elements

4) Element defines an accept method to receive a visitor object

5) ConcreteElement is a concrete element and implements the accept method

 

Core code:

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //创建ObjectStructure
        ObjectStructure objectStructure = new ObjectStructure();
        
        objectStructure.attach(new Man());
        objectStructure.attach(new Woman());
        
        
        //成功
        Success success = new Success();
        objectStructure.display(success);


A list of storing Person is maintained in ObjectStructure, attach:
    public void attach(Person p) {         persons.add(p);     }

objectStructure.display(success); This sentence:
    public void display(Action action) {         for(Person p: persons) {             p.accept(action);         }     } is to traverse all the queues in the data structure and set the operation , the operation of accept is as follows:




    @Override
    public void accept(Action action) {
        // TODO Auto-generated method stub
        action.getManResult(this);
    }

 

Then operate through action, it can be seen that such as the Success class can be added without affecting the original Object.

 

Summary of Application Cases

-Double dispatch is mentioned above. The so-called double dispatch means that no matter how the class changes, we can find the desired method to run. Double dispatch means that the action to be performed depends on the kind of request and the types of both receivers

- Taking the above example as an example, suppose we want to add a Wait status class to examine the reactions of the Man class and the Woman class. Because of the use of double dispatch, we only need to add an Action subclass to call it on the client. Change the code of any other class.

 

Notes and Details of the Visitor Pattern

advantage

1) The visitor mode conforms to the principle of single responsibility, which makes the program have excellent scalability and high flexibility

2) The visitor mode can unify the functions, and can do reports, UI, interceptors and filters, and is suitable for systems with relatively stable data structures

shortcoming

1) Specific elements publish details to visitors, that is to say, visitors pay attention to the internal details of other classes, which is not recommended by Dimiter's law, which makes it difficult to change specific elements

2) It violates the principle of dependency inversion. Visitors depend on concrete elements, not abstract elements

3) Therefore, if a system has a relatively stable data structure and frequently changing functional requirements, then the visitor mode is more appropriate.

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_22059611/article/details/103251450