For the concept of the pattern itself, please refer to other articles on the Web
This is only discussed in the application of PHP in the actual development process
This mode is extremely limited in scope and applies to scenarios:
1. Applicable to the project maintenance process, not applicable to the project development process
2. Additional requirements requiring one/more of the same/similar methods for one/More classes
3. Original code cannot be modified or extended
4. The original class has reserved an interface prepared for this mode.
Above, 3,4 is difficult at the same time in the product maintenance process simultaneously appears.
Demand:
1. A group of objects, belonging to a parent class or a different parent class, using a data structure to form a data set, where the structure can be a concept of a queue, stack, set, tree, graph or the actual meaning of a one-dimensional or multidimensional array, as long as you can iterate
2. The project needs to add an action to the above object so that the same name is called when traversing the entire set of objects.
Implementation process:
1. In the original code, the above object belongs to the class, in advance reserved an extension interface, can be called accept (strange name)
Class elementa{
Public function Accept (Visitorbase $v) {
$v->visita ($this);
}
}
Class elementb{
Public function Accept (Visitorbase $v) {
$v->VISITB ($this);
}
}
According to the above, we certainly have to define a visitorbase abstract interface beforehand.
Interface Visitorbase {
function Visita (Elementa $eleA);
function VISITB (Elementb $eleB);
}
2. In the new code, we create a class for the new operation, called Visitorx (the visitor, again a strange name), if there is another operation, can be defined as visitory, both implement the Visitorbase interface
Class Visitorx implements visitorbase{
Public Function Visita (Elementa $eleA) {
Here you can access the method of element A to handle a specific transaction
$eleA->somefunc ();
}
Public Function VISITB (Elementb $eleB) {
......
}
}
3. In the new code, we can iterate over the array as an example (this is most commonly used)
$x =new Visitorx;
foreach ($elementArray as $element) {
$element->accept ($x);
}
The above will traverse all elements (whether or not the same parent class) and perform the corresponding action in the Visitorx class for each element.
Grassroots perspective:
1. What is the original code not allowed to modify? Just add a method.
2. I do not change the original code, not let me inherit, expand a method?
3. Does the original code define an extension interface? Previous programmers were so forward-looking?
4. As long as there are other ways to achieve the same function, try not to use this design pattern, which will lead to the same object operation code scattered in different locations of the program, not conducive to further maintenance and modification.
5. Alternative implementation methods that can be considered:
A. Modifying an existing class, adding a method with the same name
B. Extending (inheriting) the original class, adding a method of the same name
C. If you must set together code for the same operation of different classes, consider trait.