This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money. Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
define
The main separation of data structures from data operations.
UML diagrams
The visitor pattern is implemented using the example of the human being being divided into men and women. The UML diagram is as follows:
1. Action
An abstract state class that mainly declares the following two methods.
The key here is that people are only divided into men and women, and the classification of this gender is stable. Therefore, two methods of “man response” and “woman response” can be added to the state class. The number of methods is stable and will not change easily.
public abstract class Action {
// Get the man's conclusion or reaction
public abstract void getManConclusion(Man man);
// Get the woman's conclusion or reaction
public abstract void getWomanConclusion(Woman woman);
}
Copy the code
2. Person
An abstract class of people. There is only one abstract method “receive”, which is used to get a “state” object.
public abstract class Person {
/ / to accept
public abstract void accept(Action action);
}
Copy the code
3. The concrete implementation class of the Action class
The Success class is used as an example, and the Fail class is used in the same way.
public class Success extends Action {
@Override
public void getManConclusion(Man man) {
System.out.println("Men succeed...");
}
@Override
public void getWomanConclusion(Woman woman) {
System.out.println("Women succeed..."); }}Copy the code
4. The concrete implementation class of the Person class
Here’s the Man, and here’s the Woman.
Double dispatch is used here, where one dispatch is completed by passing the specific state as an argument to the Man class in the client program, and then the Man class completes the second dispatch by calling getManConclusion() in the “concrete method” as an argument and passing itself (this) as an argument. The Accept method is a double dispatch operation that gets executed depending not only on the specific state of the Action class, but also on the category of Person it accesses.
public class Man extends Person {
@Override
public void accept(Action action) {
action.getManConclusion(this); }}Copy the code
5. Structure objects
public class ObjectStructure {
private List<Person> elements = new LinkedList<>();
/ / add
public void attach(Person person) {
elements.add(person);
}
/ / remove
public void detach(Person person) {
elements.remove(person);
}
// View the display
public void display(Action action) {
for(Person person : elements) { person.accept(action); }}}Copy the code
6. Client Indicates the Client
public class Client {
public static void main(String[] args) {
ObjectStructure objectStructure = new ObjectStructure();
objectStructure.attach(new Man());
objectStructure.attach(new Woman());
/ / success
Success success = new Success();
objectStructure.display(success);
/ / fail
Failing failing = newFailing(); objectStructure.display(failing); }}Copy the code