Design mode (20) Visitor mode Visitor (object behavior type)
1. Overview 
  During the 
 
   software development process, for some objects in the system, they are stored in the same collection collection and have different types, and for objects in that collection, they can be accessed by a class of objects called accessors, and different visitors have their access differently. Example 1: Customers in the supermarket will choose the goods, such as apples, books and so on in the shopping cart, and then to the cashier to pay. During the shopping process, customers are required to access these items in order to confirm the quality of these items, and then the cashier will need to access the items selected by the customer in the cart when calculating the price. At this point, the shopping cart is used as a objectstructure (object structure) to store various types of goods, and customers and cashiers, as visitors to these goods, need to check and price the goods. Different types of goods may have different forms of access, such as when Apple needs weighed and then the book does not need to be priced. 
 
 
2. Questions 
 
 
   operations on the same collection object are not unique, and there may be many different ways to manipulate the same element object. And these operations are not stable, if you need to add new operations, how to meet the new business requirements? 
 
 
3. Solution 
 
 
   Visitor Pattern: Represents an operation that acts on elements in an object's structure. It allows you to define new actions that act on these elements without changing the class of each element. 1) The object structure in the visitor pattern stores different types of element objects for different visitors to access. 2) The visitor pattern consists of two hierarchies, one for the visitor hierarchy, for the abstract visitor and for the specific visitor, and one for the element hierarchy, providing abstract elements and concrete elements. The same visitors can access different elements in different ways, and the same elements can be accessed in different ways by different audiences. In the visitor mode, adding new visitors does not need to modify the original system, the system has a good 
 
 scalability 
4. Applicability 
 
 
   Use VI s i t o r mode in the following cases: 
  
• An object structure contains many classes of objects that have different interfaces, and you want to implement some operations that depend on their specific classes for those objects. 
  
• You need to do many different and unrelated operations on objects in an object structure, and you want to avoid classes that let these actions "pollute" those objects. Visitor allows you to centralize related operations into a class. When the object structure is shared by many applications, use visitor mode to have each app contain only the actions that need to be used. 
  
• Classes that define object structures rarely change, but it is often necessary to define new operations on this structure. Changing the object structure class requires redefining the interface to all visitors, which can take a significant cost. If the object structure classes often change, it might be better to define them in these classes. 
 
 
5. Structure 
 
  
6. Composition of the pattern 
  The 
 
   visitor pattern includes the following roles: 
  
Abstract Visitor (Vistor):-Declares a visit operation for each class concreteelement in the object structure. The name of the operation and the special 
  
Identifies the class that sent the visit request to the visitor. This allows the visitor to determine which element is being accessed 
  
The specific class. This allows the visitor to access it directly through the element's specific interface. 
  
Specific visitor (Concretevisitor):-Implements each action declared by visitor. Each operation implements part of the algorithm, and the algorithm fragment is 
  
The class that corresponds to the object in the structure. Concretevisitor provides the context for the algorithm and stores its local state. 
  
This state often accumulates results in the process of traversing the structure. 
  
Abstract element: Defines an accept operation that takes a visitor as a parameter. 
  
specific element (Concreteelement): Implements the accept operation, which takes a visitor as a parameter. 
  
Object Structure (objectstructure): An element that can enumerate it. You can provide a high-level interface to allow the visitor to access its elements. Can be a composite or a collection, such as a list or an unordered collection. 
 
 
7. Effects 
 
  
  Advantages of the visitor pattern:• makes it easy to add new access operations. If some operations depend on a complex structure object, it is generally complicated to add new operations. By using the visitor pattern, adding new actions means adding a new visitor class, so it becomes easy. • Centralize the access behavior of the element objects into a single visitor object instead of being scattered across the element classes. • Visitor Patterns You can access member classes that belong to different hierarchy structures across hierarchical hierarchies of several classes. An iteration child can only access member objects that belong to the same type hierarchy, and cannot access objects belonging to different hierarchical structures. The visitor pattern can do this. • Allows the user to define the operation of the class hierarchy without modifying the existing class hierarchy. 
  Disadvantages of the visitor pattern:• It is difficult to add new element classes. In visitor mode, each addition of a new element class means adding a new abstraction to the abstract visitor role and adding specific actions to each specific visitor class, violating the requirement of the "open and close principle". • Destroy the package. The visitor pattern requires the visitor object to access and invoke the actions of each element object, which means that the element object must sometimes expose its own internal operations and internal State, otherwise it cannot be accessed by the visitor. 
 
 
8. Implement 
 
 
 We are. Phppan provides an example:
  
 
 
  
 [PHP]View Plain Copy print? 
  
  
  - <?php
- /**
- * Visitor Mode
- * @author Guisu
- *
- */
- Interface Visitor {
- Public function Visitconcreteelementa (Concreteelementa $elementA); 
- Public function Visitconcreteelementb (concreteelementb $elementB); 
- }
- Interface Element {
- Public function Accept (Visitor $visitor); 
- }
- /**
- * Specific Visitors 1
- */
- Class ConcreteVisitor1 implements Visitor {
- Public function Visitconcreteelementa (Concreteelementa $elementA) {  
- echo $elementA->getname (),' visitd by ConcerteVisitor1 <br/> '; 
- }
- Public function Visitconcreteelementb (concreteelementb $elementB) {  
- echo $elementB->getname ().' visited by ConcerteVisitor1 <br/> '; 
- }
- }
- /**
- * Specific Visitors 2
- */
- Class ConcreteVisitor2 implements Visitor {
- Public function Visitconcreteelementa (Concreteelementa $elementA) {  
- echo $elementA->getname (), ' visitd by ConcerteVisitor2 <br/> '; 
- }
- Public function Visitconcreteelementb (concreteelementb $elementB) {  
- echo $elementB->getname (), ' visited by ConcerteVisitor2 <br/> '; 
- }
- }
- /**
- * Specific Element a
- */
- Class Concreteelementa implements Element {
- private$_name; 
- Public function __construct ($name) {  
- $this->_name =$name; 
- }
- Public function GetName () { 
- return$this->_name; 
- }
- /** 
- * Accept the new method that the visitor calls it against the element
- * @param Visitor $visitor
- */
- Public function Accept (Visitor $visitor) {  
- $visitor->visitconcreteelementa ($this); 
- }
- }
- /**
- * Specific element b
- */
- Class Concreteelementb implements Element {
- private$_name; 
- Public function __construct ($name) {  
- $this->_name =$name; 
- }
- Public function GetName () { 
- return$this->_name; 
- }
- /** 
- * Accept the new method that the visitor calls it against the element
- * @param Visitor $visitor
- */
- Public function Accept (Visitor $visitor) {  
- $visitor->visitconcreteelementb ($this); 
- }
- }
- /**
- * Object structure is a collection of elements
- */
- Class Objectstructure {
- private$_collection; 
- Public function __construct () { 
- $this->_collection =Array (); 
- }
- Public function Attach (Element $element) {  
- Returnarray_push ($this->_collection,$element); 
- }
- Public function Detach (Element $element) {  
- $index =array_search ($element,$this->_collection); 
- if ($index!==false) { 
- unset ($this->_collection[$index]); 
- }
- return$index; 
- }
- Public function Accept (Visitor $visitor) {  
- foreach ($this->_collection as$element) {   
- $element->accept ($visitor); 
- }
- }
- }
- Class Client {
- /** 
- * Main program.
- */
- public static function main () {  
- $elementA = new Concreteelementa ("Elementa"); 
- $elementB = new Concreteelementb ("ELEMENTB"); 
- $elementA 2 = new Concreteelementb ("ElementA2"); 
- $visitor 1 = new ConcreteVisitor1 (); 
- $visitor 2 = new ConcreteVisitor2 (); 
- $os = new Objectstructure (); 
- $os->attach ($elementA); 
- $os->attach ($elementB); 
- $os->attach ($elementA 2); 
- $os->detach ($elementA); 
- $os->accept ($visitor 1); 
- $os->accept ($visitor 2); 
- }
- }
- Client::main ();
- ?>
9. With other related modes 
 
 
   • Iterator mode) because the visitor pattern requires manipulation of the object structure, and the object structure itself is a collection of element objects, the visitor pattern often needs to be associated with the iterator pattern, using iterators in the object structure to traverse the element object. • Combined mode) in visitor mode, the element object may exist in the container object and the leaf object, so it can be designed in combination with the combined mode. 
 
 
10. Expansion 
  The tilted "open and closed 
 
   principle" • Visitor mode supports the "open and close" principle in a slanted manner, adding new visitors to the convenience, but adding new elements is difficult. The most important thing in object-oriented design principles is the so-called "open and close" principle. The design of a software system should be as far as possible open to the extension, the modification is closed. The way to achieve this principle is to follow the principle of "encapsulation of Change". The principle is that when designing a software system, you should try to identify the parts of a software system that will change and encapsulate them. 
  
Many systems can be separated by algorithms and data structures, which means that some objects contain algorithms, while others contain data and accept the operation of the algorithm. If such a system has a relatively stable data structure, and there are easy-to-change algorithms, the use of the visitor pattern is more appropriate, because the visitor pattern makes the operation of the algorithm more easy. 
  
Conversely, if such a system's data structure objects are prone to change, it is often not appropriate to use the visitor pattern when new data objects are added.  Because it is difficult to add new nodes in the visitor pattern, it involves adding new methods to abstract visitors and all specific visitors. 
 
 
10. Summary and Analysis
Design mode (20) Visitor mode Visitor (object behavior type)