define

Represents an operation that operates on elements in an object structure, allowing you to define new operations that operate on those elements without changing their classes.

nature

Reserved path, callback implementation

His role

  • Visitor (Visitor)

    Interface, or abstract class, defines the behavior of accessing each element, and its parameters are the elements that can be accessed. The number of methods is theoretically the same as the number of elements, so the Visitor pattern requires that the class family of elements be stable. If elements are added and removed frequently, this will inevitably result in frequent changes to the Visitor interface, indicating that the Visitor pattern is not suitable for use.

  • ConcreteVisitor (ConcreteVisitor)

    The concrete visitor, who needs to give the concrete behavior generated when accessing each element class.

  • Element

    An element interface, or abstract class, that defines a method that accepts visitors, meaning that each element must be accessible to visitors.

  • ConcreteElement

    A concrete element that provides a concrete implementation of a method that accepts visitors

  • ObjectStructure

    An object structure is an abstract representation that internally manages a collection of elements and iterates over which elements are accessed by visitors.

The sample code

/** * public class Staff {public String name; public int kpi; public Staff(String name) { this.name = name; kpi = new Random().nextInt(10); } @param Visitor */ public void accept(Visitor Visitor); } /** * public class Engineer extends Staff{public Engineer(String name) {super(name); } @Override public void accept(Visitor visitor) { visitor.visit(this); } @return */ public int getCodeLines(){return new Random().nextint (10000); Public class Manager extends Staff{public Manager(String name) {super(name);}} public class Manager extends Staff{public Manager(String name) {super(name); } @Override public void accept(Visitor visitor) { visitor.visit(this); } @return */ public int getProducts(){return new Random().nextint (100); } } public class BusinessReport { public List<Staff> mStaffs = new ArrayList<>(); Public BusinessReport() {mStaffs. Add (new Manager(" wang Manager ")); MStaffs. Add (New Engineer(" Engineer 1")); MStaffs. Add (New Engineer(" Engineer 2")); } /** * public void showReports(Visitor Visitor){for (Staff Staff: mStaffs){Staff. Accept (Visitor); /** * CEOVisitor implements Visitor{@override public void */ CEOVisitor implements Visitor{@override public void Visit (Engineer) {system.out. println(" Engineer: "+ Engineer. Name + ", KPI:" + Engineer. } @override public void visit(Manager Manager) {system.out.println (" Manager: "+ manager.name + ", KPI: "+ manager.kpi + ", number of new products:" + Manager.getProducts ()); } /** * implements Visitor{public class CTOVisitor implements Visitor{@override public void Visit (Engineer) {system.out.println (" Engineer: "+ Engineer. Name + ", line count:" + Engineer. } @override public void visit(Manager Manager) {system.out.println (" Manager: "+ manager.name + ", " + manager.getProducts()); Public class Client {public static void main(String[] args){BusinessReport BusinessReport = new BusinessReport(); System.out.println("====== for CEO ======="); businessReport.showReports(new CEOVisitor()); System.out.println("====== for CTO ======="); businessReport.showReports(new CTOVisitor()); }}Copy the code

The results

====== Report for CEO ======= Manager: Manager Wang, KPI: 9, Number of new products: 6 Engineer: Engineer 1, KPI: 0 Engineer: Engineer 2, KPI: 9 ====== Report for CTO ======= Manager: Wang, Product quantity: 43 Engineer: Engineer 1, lines of code: 9902 Engineer: Engineer 2, lines of code: 5611Copy the code

function

The visitor pattern allows you to transparently add new functionality to a set of objects, avoiding changes to the set of objects during maintenance, and also allows you to reuse functionality that visitors have.

advantages

  • Good extensibility, the ability to add new functionality to elements in the object structure without modifying the object structure.
  • Good reusability improves reuse by allowing visitors to define common functions across the object structure.
  • Separate irrelevant behavior, you can separate irrelevant behavior through the visitor, the related behavior encapsulated together, constitute a visitor, so that each visitor’s function is relatively single.

disadvantages

  • The object structure is difficult to change, not the case where classes in the English object structure often change, because the object structure changes, the interface of the visitor and the implementation of the visitor have to change accordingly, the cost is too high.
  • Breaking encapsulation. The visitor pattern usually requires objects to open up their internal data to visitors and object structures, which breaks the encapsulation of objects.

Usage scenarios

  • If you want to perform operations on an object structure that depend on concrete classes in the object structure, you can use the visitor pattern.
  • If you want to perform many different and unrelated operations on each element of an object structure, you can avoid cluttering the class with these operations by using the visitor pattern, which separates these operations into different visitor objects so that each visitor object implements the same kind of functionality.
  • The visitor pattern can be used if the object structure rarely changes, but you often need to define new operations on the element objects in the object.