Visitor mode: encapsulate some operations that act on each element in a certain data structure. It can define new operations that act on these elements without changing this data structure. The structure diagram of the visitor mode is as follows:
From the above picture, you can see that he has the following characters:
Abstract Visitor role: defines an interface and declares one or more access operations.
Concrete Visitor role: implements the interface declared by abstract visitors, that is, the various access operations declared by abstract visitors.
Abstract element (Visitable) role: declares an accept operation, accepting a visitor object as a parameter.
ConcreteElement role: implements the acceptance operation specified by abstract nodes.
Data structure object (ObjectStructure) role: It can traverse all elements in the structure and provide an interface for the visitor object to access each element.
The simulation code is as follows:
interface Visitor { void visit(Gladiolus g); void visit(Chrysanthemum c); } // concrete visitor name access class StringVisitor implements Visitor { String s; public String toString() { return s; } public void visit(Gladiolus g) { s = "Gladiolus"; } public void visit(Chrysanteumum c) { s = "Chrysanteumum"; } } // concrete visitor class BeeVisitor implements Visitor { public void visit(Gladiolus g) { System.out.println("Bee to access Gladiolus"); } public void visit(Chrysanteum c) { System.out.println("Bee to access Chrysanteum"); } } interface Flower { void accept(Visitor v); } /* * concrete element Chrysanthemum implements Flower { public void accept(Visitor v) { v.visit(this); } } // concrete element class Gladiolus implements Flower { public void accept(Visitor v) { v.visit(this); } } //This is a Flower object generator class FlowerGenerator { private static Random rand = new Random(); public static Flower newFlower() { switch (rand.nextInt(2)) { default: case 0: return new Gladiolus(); case 1: return new Chrysanthemum(); } } } public class Test { /* * First, get a specific visitor role on the client. The object structure is traversed. Call the accept method for each element, and pass the specific visitor role in. This completes the entire process*/ public static void main(String args[]) { List<Flower> flowers = new ArrayList<Flower>(); for (int i = 0; i < 10; i++) flowers.add(FlowerGenerator.newFlower()); Visitor visitor = new StringVisitor(); Iterator<Flower> iterator = flowers.iterator(); while (iterator.hasNext()) { iterator.next().accept(visitor); System.out.println(visitor); } System.out.println("-----------------------"); /* * A new access behavior: BeeVisitor Bee access*/ Visitor visitor2 = new BeeVisitor(); for (Flower flower : flowers) { flower.accept(visitor2); } } }result:
Gladiolus Chrysanthemum Chrysanthemum Gladiolus Chrysanthemum Chrysanthemum Chrysanthemum Chrysanthemum Gladiolus Gladiolus ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Visitor mode can be considered if:
1. An object structure contains many class objects, and they have different interfaces, and you want to implement some operations that depend on their specific classes on these objects.
2. You need to perform many different and irrelevant operations on objects in an object structure, and you want to avoid letting these operations "pollut" the classes of these objects. Visitor allows you to concentrate related operations and define them in a class.
3. When the object structure is shared by many applications, use the Visitor mode to allow each application to contain only the operations that need to be used.
4. The classes that define the object structure rarely change, but new operations are often needed to define this structure. Changing the object structure class requires redefining the interface to all visitors, which can be a big cost. If the object structure class changes frequently, it may be better to define these operations in these classes.
These individuals seem to be suggestions, and specific issues need to be analyzed in the project.