The visitor pattern of java designer pattern (14)

The visitor pattern of java designer pattern (14)

The visitor pattern of java designer pattern

The real life is to take care of your daily tasks. Don't ask for love and prospects that have nothing to do with you at the moment. Don't dwell on unnecessary emotions and judgments. No delusion, no self-indulgence in it. Don't hurt yourself or be hostile to yourself and others. Do not perform, and do not believe in other people's performances.

Design pattern learning, I will blog about 23 design patterns in the near future , so stay tuned~
2021/1/17

definition

Represents an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the class of each element.

Baidu Encyclopedia

Application scenarios

Assuming that the final exam results are to be announced:

The scores are divided into: Chinese mathematics
students are divided into: Xiao Ming, Xiao Hong

If you add a subject or add a student in the traditional way, you have to change the original code, which violates the opening and closing principle (open for extension and closed for modification), here is the visitor mode

UML (1.1):

  • Red box: interviewee (score)
  • Yellow box: visitor (student)
  • ObjectStructure aggregates grades and students. When calling, you can interact with ObjectStructure.

Role analysis

  • Achievement(): Interviewee interface

  • Chinese()/Mathematics(): Interviewee interface implementation

  • Student(): visitor interface

  • XiaoMing()/XiaoHong(): visitor interface implementation

  • ObjectStructure(): Structure object role, which is a necessary role to use the visitor model. It has the following characteristics: it can enumerate its elements; it can provide a high-level interface to allow visitors to access its elements; if necessary, it can be designed as a composite object or a collection (such as a list or unordered collection).

       ObjectStructure(): , 
      ( )
      
       :
      	 , ( )
     

Code

Achievement interface:

public interface Achievement {
    //
    void fraction();
}
 

Chinese language score, realize the score interface:

public class Chinese implements Achievement {
    //
    private final String name;
    //
    private final double fraction;
    // 
    public Chinese(String name ,double fraction) {
        this.fraction = fraction;
        this.name = name;
    }
    @Override
    public void fraction() {
        Log.i(" ",name+"   "+fraction+"  ");
    }
}
 

Mathematics math scores, realize the score interface:

public class Mathematics implements  Achievement {
    //
    private final String name;
    //
    private final double fraction;

    public Mathematics(String name ,double fraction) {
        this.fraction = fraction;
        this.name = name;
    }
    @Override
    public void fraction() {
        Log.i(" ",name+"   "+fraction+"  ");
    }
}
 

Student student interface, aggregate the score interface:

public interface Student {
    //
    void student(Achievement achievement);
}
 

XiaoMing Xiaoming, implement the student interface:

public class XiaoMing implements Student {
    @Override
    public void student(Achievement achievement) {
        //
        achievement.fraction();
    }
}
 

XiaoHong Xiaohong, realize the student interface:

public class XiaoHong implements Student {

    @Override
    public void student(Achievement achievement) {
        achievement.fraction();
    }
}
 

ObjectStructure, the role of structured objects, (in compliance with Dimit's principle):

public class ObjectStructure {
    /**
     * //
     * @param student  
     * @param achievement  
     */
    public void outputAchievement(Student student,Achievement achievement){
            student.student(achievement);
    }
}
 

Test code (client):

		 //
        XiaoMing xiaoMing = new XiaoMing();
        //
        XiaoHong xiaoHong = new XiaoHong();


        //
        Chinese chinese1 = new Chinese(" ",67);

        //
        Chinese chinese2 = new Chinese(" ",100);

        //
        Mathematics mathematics = new Mathematics(" ",43);
		
		//
        ObjectStructure objectStructure = new ObjectStructure();

        xiaoMing.student(chinese1);
        objectStructure.outputAchievement(xiaoMing,chinese1);
        objectStructure.outputAchievement(xiaoHong,chinese2);
        objectStructure.outputAchievement(xiaoMing,mathematics);
 

Log (2.1):

How to expand

For example, now you need to add an English, a classmate named Xiao Zhang:

English English score, achieve the score interface:

public class English implements Achievement{
    //
    private final String name;
    //
    private final double fraction;

    public English(String name ,double fraction) {
        this.fraction = fraction;
        this.name = name;
    }
    @Override
    public void fraction() {
        Log.i(" ",name+"   "+fraction+"  ");
    }
}
 

XiaoZhang student, implement the student interface:

public class XiaoZhang implements Student {

    @Override
    public void student(Achievement achievement) {
        achievement.fraction();
    }
}
 

Test code (client):

 //
XiaoZhang xiaoZhang = new XiaoZhang();
//
 English english = new English(" ", 55);
 
ObjectStructure objectStructure = new ObjectStructure();

objectStructure.outputAchievement(xiaoZhang,english);
 

Log (2.2):

Summary:

  • Dimitri comply with the principle (only with direct communication with friends, a class of their dependent classes know better, do not divulge information) This refers to ObjectStructure
  • Comply with the opening and closing principle (open for extension, closed for modification): here refers to the addition of English and Xiao Zhang extension , good scalability
  • Comply with the single responsibility principle : (One class is responsible for one responsibility, not one class is responsible for one responsibility), here refers to the achievement class corresponding to the grade/student

Complete code

Go to Design Mode Homepage/Design Principles

Originality is not easy, your likes are your greatest support for me~