Visitor mode in PHP design mode

Source: Internet
Author: User
: This article mainly introduces the visitor mode of the PHP design mode. if you are interested in the PHP Tutorial, refer to it. The visitor mode allows external classes to obtain the objects of each node in the tree structure and operate on each object. it allows us to expand functions without modifying the original tree structure, such as statistics.

In this mode, the following elements are required:

1. the specific element object, the location where the visitor actually wants to access (that is, the node)

2. stable tree structure. each node is an element object, which is usually many in combination mode, it provides the actual location for visitors to access (that is, the visitor accesses the instantiated object of a node of a specific attribute structure );

3. the visitor interface defines the visitor interface method, which is used on each node. it is used to reference the visitor at the node so that the visitor can access the current node.

4. the visitor's specific implementation inherits the visitor's interface to implement the interface method

// Define the element interface abstract class User {public function getPoint () {return rand (); // modify the data to be read by the database, here we directly simulate a value,} // The accept method here is used to introduce visitors. In this method, $ visitor visitors can obtain the required data through the User class for corresponding operations. Abstract function accept (UserVisitor $ visitor);} // defines the element interface class VipUser extends User {// getPoint () the specific implementation is implemented by the interface // here the current object is passed to the visitor of the visitor, in the visitVip method of the visitor class, the necessary data can be obtained based on $ this for the corresponding public function accept (UserVisitor $ vitor) operation) {$ vitor-> visitVip ($ this) ;}} class NormalUser extends User {// same as getPoint () the specific implementation is implemented by the interface // same as the accept public function accept (UserVisitor $ vitor) {$ vitor-> visitNormal ($ this );}} // define the visitor interface abstract class UserVisitor {// interface method that the visitor must implement to access different users abstract function visitVip (User $ user ); abstract function visitNormal (User $ user);} // the visitor of the point operation implements class PointActVisitor extends UserVisitor {public function visitVip (User $ user) {echo 'VIP User + 10 points
';} Public function visitNormal (User $ user) {echo 'normal User + 5 points
';}} // User tree structure class Users {protected $ users; public function addUser (user $ user) {$ this-> users [] = $ User ;}}

The above introduces the visitor mode of the PHP design mode, including the content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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.