The visitor pattern is actually the pattern that allows the external class to get the object of each node of the tree structure, manipulating each object, which allows us to extend functionality without altering the original tree structure, such as statistics, and so on.
In this mode, there are several elements that must be available:
1, the specific element object, the visitor actually to access the location (that is, the node)
2, the stable tree structure, each node is an element object, generally in the combination mode is more, it provides to allow visitors to be able to access the actual location (that is, the visitor is accessing the specific attribute structure of a node of the instantiation of the object);
3, the visitor interface, here defines the visitor's interface method, which is a method used in each node, to refer to the visitor at the node, so that visitors can access the current node.
4, the visitor's concrete realization, inherits the visitor interface, realizes the interface method
Defines the element interface abstract class user{public function GetPoint () {return rand ();//The data should be read by the database, which directly simulates a value,} Here the Accept method is used to introduce the visitor, in this method, $visitor the visitor can obtain the necessary data through the user class to carry on the corresponding operation. Abstract function Accept (Uservisitor $visitor);} Define the element interface class Vipuser extends user{///Here GetPoint () implementation is implemented by the interface//Here the current object is passed to the visitor visitor, in the visitor class Visitvip method can be based on $thi s get the necessary data for the appropriate operation public function accept (Uservisitor $vitor) {$vitor->visitvip ($this); }}class Normaluser extends user{///IBID. GetPoint () implementation is implemented by the interface//with the Vipuser class in the Accept public function accept (uservisit or $vitor) {$vitor->visitnormal ($this); }}//defines the visitor interface abstract class uservisitor{//visitors must implement an interface method that accesses different users abstract function visitvip (user $user); Abstract function Visitnormal (User $user); }//Integration Operation Visitor Implementation class Pointactvisitor extends uservisitor{public function visitvip (user $user) {echo ' VIP user +10 Score of
'; } Public Function Visitnormal (user $user) {echo ' normal user +5 points
'; }}//user tree-shaped structure class users{protected $users; Public Function AddUser (User $user) {$this->users[] = $user; }}
The above describes the PHP design pattern of the visitor pattern, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.