Visitor mode

Posted by ondemand on Thu, 03 Feb 2022 13:15:05 +0100

Visitor mode

Basic introduction to visitor mode

  1. Visitor Pattern encapsulates some operations that act on each element of a data structure. It can define new operations that act on these elements without changing the data structure.
  2. It mainly separates data structure and data operation, and solves the problem of data structure and operation coupling
  3. The basic working principle of visitor mode is: add an interface to the visited class to receive visitors
  4. The main application scenarios of visitor mode are: many different operations need to be performed on objects in an object structure (these operations are not related to each other), and
    If you need to avoid these operations "polluting" the classes of these objects, you can choose visitor mode to solve it

Schematic class diagram of visitor pattern

Description of schematic class diagram - i.e. (roles and responsibilities of visitor pattern)

  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 access value to implement each operation declared by Visitor, which 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

Application example requirements
1) Divide people into men and women and evaluate singers. After watching a singer's performance, get their different evaluations of the singer (there are different types of evaluations, such as success and failure). Please use the visitor mode to realize it

2) Train of thought analysis and illustration (class diagram)

package com.zhangqi.day1.visitor;

/**
 * @author: zhangqi
 * @create: 2022/2/3 17:33
 */
public abstract class Person {
    //Provide a method that visitors can access
    public abstract void accept(Action action);
}

package com.zhangqi.day1.visitor;

/**
 * explain
 * 1. Here we use double dispatch, that is, first, in the client program, the specific state is passed to Woman as a parameter (the first dispatch)
 * 2. Then, the Woman class calls the method getWomanResult in the "concrete method" as a parameter, and takes itself (this) as a parameter
 * Pass in and complete the second dispatch
 *
 * @author: zhangqi
 * @create: 2022/2/3 17:33
 */
public class Man extends Person {

    @Override
    public void accept(Action action) {
        action.getManResult(this);
    }

}

package com.zhangqi.day1.visitor;

/**
 * explain
 * 1. Here we use double dispatch, that is, first, in the client program, the specific state is passed to Woman as a parameter (the first dispatch)
 * 2. Then, the Woman class calls the method getWomanResult in the "concrete method" as a parameter, and takes itself (this) as a parameter
 * Pass in and complete the second dispatch
 *
 * @author: zhangqi
 * @create: 2022/2/3 17:33
 */
public class Woman extends Person {

    @Override
    public void accept(Action action) {
        action.getWomanResult(this);
    }

}

package com.zhangqi.day1.visitor;

/**
 * @author: zhangqi
 * @create: 2022/2/3 17:32
 */
public abstract class Action {

    //Get male evaluation
    public abstract void getManResult(Man man);

    //Get female evaluation
    public abstract void getWomanResult(Woman woman);

}
      
package com.zhangqi.day1.visitor;

/**
 * @author: zhangqi
 * @create: 2022/2/3 17:34
 */
public class Success extends Action {

    @Override
    public void getManResult(Man man) {
        System.out.println(" The man rated the singer as successful !");
    }


    @Override
    public void getWomanResult(Woman woman) {
        System.out.println(" The woman rated the singer as successful !");
    }
}


package com.zhangqi.day1.visitor;

/**
 * @author: zhangqi
 * @create: 2022/2/3 17:34
 */
public class Fail extends Action {

    @Override
    public void getManResult(Man man) {
        System.out.println(" The evaluation given by the man failed the singer !");
    }


    @Override
    public void getWomanResult(Woman woman) {
        System.out.println(" The singer failed in the evaluation given by the woman !");
    }
}


package com.zhangqi.day1.visitor;

/**
 * @author: zhangqi
 * @create: 2022/2/3 17:37
 */
public class Client {
    public static void main(String[] args) {
        //Create ObjectStructure
        ObjectStructure objectStructure = new ObjectStructure();

        objectStructure.attach(new Man());
        objectStructure.attach(new Woman());

        //success
        Success success = new Success();
        objectStructure.display(success);

        System.out.println("===============");
        Fail fail = new Fail();
        objectStructure.display(fail);


    }
}


Topics: Design Pattern