This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

A scenario

Interviewer: It says on your resume that you are familiar with design patterns. What is the visitor pattern?

Yang Lele: Well… No (such a cold one, who knows what that is)

Interviewer: Goodbye too late to shake hands

Yang Lele, do you know what I don’t know? I think a lot of people are not clear, yes, he is indeed relatively unpopular, but I do not think that the commonly used things, does not mean that there is no value, learn more about more, may also be the most important part of the accumulated development.

In the process of programming, I have more or less experienced the experience of learning a lot of things, but I seem to have no use for them, and then one day, I receive a function, which is to use the technology I have known before. Maybe this is your chance but I don’t know.

In fact, design patterns are a well-known tool for writing code, but a lot of people don’t really look into them. Why? Because design patterns don’t play a major role in interviews in most courses, or even in many technology stacks. Maybe this is the industry status quo, latest too much while ignoring the foundation, many programmers with video unsanitary environment, comfortable video look down, but found that learned the most is the surface, most is its, without the reason, why, because not foundation, formation, and because of learning to program, need to not just look, it’s more important to do.

Two Visitor Pattern

As the name suggests, it has to do with visitors, meaning that you pass in different visitors, and then different visitors do the same thing, but get different results, and in the code, different visitors access the same method, but return different results, not the same method.

Public interface Visitor {// Access Student information void visit(Student Student); Void visit(Teacher Teacher); }Copy the code

We see an abstract class with a visitor who visits students and teachers. But the names of the methods are the same

// Parent public class Parent implements Visitor {private Logger Logger = LoggerFactory. Public void visit(Student Student) {logger.info(" {}", student.name, student.clazz, student.ranking()); } public void visit(Teacher Teacher) {logger.info(" Teacher info name: {} class: {} level: {}", teacher.name, teacher.clazz, teacher.identity); } } public class Principal implements Visitor { private Logger logger = LoggerFactory.getLogger(Principal.class); Public void visit(Student Student) {logger.info(" Student name: {} class: {}", student.name, student.clazz); } public void visit(Teacher Teacher) {logger.info(" student info name: {} class: {} Enrollment: {}", teacher.name, teacher.clazz, teacher.entranceRatio()); }}Copy the code

And in this case, the parents and the principal,

Here are the interviewees

public class Student extends User { public Student(String name, String identity, String clazz) { super(name, identity, clazz); } public void accept(Visitor visitor) { visitor.visit(this); } public int ranking() { return (int) (Math.random() * 100); Public void show(Visitor Visitor) {for (User User: userList) {user.accept(Visitor); }}Copy the code

This completes the use of a visitor pattern, the most important of which is the advantage that different visitors call the same method name and get different results.