This design pattern series, all for the use of easy to understand language, not in-depth, recently in the system to learn ASM bytecode framework, which used a variety of Visitor classes, a large number of applications of the Visitor design pattern, special record this article.

1. Basic Introduction:

  • Encapsulates operations that operate on elements of a data structure and defines new operations that operate on those data elements without changing the data structure.
  • Main solution: decoupling of stable data structures and volatile operations.
  • If there are operations on an object that are irrelevant (or weakly related) to the object, you can use the visitor pattern to encapsulate these operations in the visitor, thus preventing the unrelated operations from contaminating the object.

2. Members:

  • One is called an Element and the other is called a Visitor.
  • The element has an Accept () method and the visitor has a visit() method.
  • Element# Accept (Visitor Visitor) : The element’s Accept () takes a Visitor as an argument, and the Visitor’s visit() method is called in this method, where the element itself is passed as an argument and the element itself decides whether to visit.

3. Specific:

3.1 Scenario:

  • If the teacher’s teaching feedback score is greater than or equal to 85 points, and the student’s score is greater than or equal to 90 points, the student can be selected for excellence award. If the number of teacher papers is more than 8 and the number of student papers is more than 2, they can be selected for the Research Excellence Award.
  • In this case, the teacher and the student are elements, and their data structures are stable. From the above description, we can see that the operation on the data structure is variable, and the results are evaluated one moment, and the research is evaluated the next, which makes it appropriate to use the visitor pattern to separate the data structure from the operation.

3.2. Implementation:

Create abstract Element Element
public interface Element {
	    void accept(Visitor visitor);
}
Copy the code
3.2.2 Create a concrete Element: Create two concrete elements Student and Teacher to implement the Element interface respectively
public class Student implements Element {
	    private String name;
	    private int grade;
	    private int paperCount;
	 
	    public Student(String name, int grade, int paperCount) {
	        this.name = name;
	        this.grade = grade;
	        this.paperCount = paperCount;
	    }
	 
	    @Override
	    public void accept(Visitor visitor) {
	        visitor.visit(this); }... }public class Teacher implements Element {
	    private String name;
	    private int score;
	    private int paperCount;
	 
	    public Teacher(String name, int score, int paperCount) {
	        this.name = name;
	        this.score = score;
	        this.paperCount = paperCount;
	    }
	 
	    @Override
	    public void accept(Visitor visitor) {
	        visitor.visit(this); }... }Copy the code
3.2.3 Create abstract Visitor Visitor
	public interface Visitor {
	 
	    void visit(Student student);
	 
	    void visit(Teacher teacher);
	}
Copy the code
3.2.4 Create specific Visitor: Create a specific Visitor GradeSelection based on the score to implement the Visitor interface
public class GradeSelection implements Visitor {
	 
	    @Override
	    public void visit(Student student) {
	        if(student ! =null && student.getGrade() >= 90) {
	            System.out.println(student.getName() + "The score is" + student.getGrade() + "And won the award for excellence."); }}@Override
	    public void visit(Teacher teacher) {
	        if(teacher ! =null && teacher.getScore() >= 85) {
	            System.out.println(teacher.getName() + "The score is" + teacher.getScore() + "And won the award for excellence."); }}}Copy the code
3.2.5 Visitor code Call:
	public class VisitorClient {
	 
	    public static void main(String[] args) {
	        Element element = new Student("lunn".98.5);
	 
	        Visitor visitor = newGradeSelection(); element.accept(visitor); }}Copy the code