The visitor pattern for PHP design mode

Source: Internet
Author: User
Tags php tutorial
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.