PHP design mode-Visitor mode _ PHP Tutorial

Source: Internet
Author: User
PHP design mode-Visitor mode. PHP design pattern-Visitor pattern indicates an operation that acts on each element in an object structure. It allows you to define the PHP design pattern-Visitor pattern without changing the element classes.

Visitor mode indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the element classes.

UML class diagram:

Role:

1. abstract visitor (State): declares an access operation interface for the specific element role in the object structure. The name and parameters of this operation interface identify the specific element role that sends an access request to a specific visitor, so that the visitor can directly access it through the specific interface of this element role.

2. specific visitor (Success): implements the interface declared by the visitor.

3. abstract element (Person): defines an accept () for an access request, which uses a visitor as a parameter.
4. specific element (Man): implements the acceptance operation interface defined by the abstract element.

5. Structure object (ObjectStruct): This is a required role to use the visitor mode. It has the following features: it can enumerate its elements; it can provide a high-level interface to allow visitors to access its elements; if necessary, it can be designed as a composite object or an aggregation (such as a list or unordered set ).

Core code:

 State_name = successful;} public function GetManAction (VMan $ elementM) {echo {$ elementM-> type_name }:{$ this-> state_name, most of them have a great woman .;} When the public function GetWomanAction (VWoman $ elementW) {echo {$ elementW-> type_name }:{$ this-> state_name}, most of the men behind the scenes are unsuccessful .;} // Failed status class Failure extends State {public function _ construct () {$ this-> state_name = Failed;} public function GetManAction (VMan $ elementM) when {echo {$ elementM-> type_name }:{$ this-> state_name}, no one needs to persuade you to drink alcohol .;} When the public function GetWomanAction (VWoman $ elementW) {echo {$ elementW-> type_name }:{$ this-> state_name}, no one can persuade you .;} // Love status class Amativeness extends State {public function _ construct () {$ this-> state_name = ;} public function GetManAction (VMan $ elementM) {echo {$ elementM-> type_name }:{$ this-> state_name}, you must understand everything you do not understand .;} When the public function GetWomanAction (VWoman $ elementW) {echo {$ elementW-> type_name }:{ $ this-> state_name}, you must pretend not to understand the case .;} // Man class VMan extends Person {function _ construct () {$ this-> type_name = man;} public function Accept (State $ visitor) {$ visitor-> GetManAction ($ this) ;}// female class VWoman extends Person {public function _ construct () {$ this-> type_name = female ;} public function Accept (State $ visitor) {$ visitor-> GetWomanAction ($ this) ;}// object structure class ObjectStruct {private $ elements = array (); // Add public function Add (Person $ element) {array_push ($ this-> elements, $ element);} // Remove public function Remove (Person $ element) {foreach ($ this-> elements as $ k => $ v) {if ($ v = $ element) {unset ($ this-> elements [$ k]) ;}}// view the public function Display (State $ visitor) {foreach ($ this-> elements as $ v) {$ v-> Accept ($ visitor );}}}

Test client code:

Header (Content-Type: text/html; charset = utf-8); // ------------------------ Visitor mode -------------------- require_once. /Visitor. php; $ OS = new ObjectStruct (); $ OS-> Add (new VMan (); $ OS-> Add (new VWoman ()); // response $ ss = new Success (); $ OS-> Display ($ ss); // response $ fs = new Failure () upon Failure (); $ OS-> Display ($ fs); // reaction in Love $ ats = new Amativeness (); $ OS-> Display ($ ats );

Applicable scenarios and advantages:

1) an object structure contains many class objects with different interfaces. you want to perform operations on these objects dependent on their specific classes.

2) you need to perform many different and unrelated operations on the objects in an object structure, and you want to avoid causing these operations to "pollute" the classes of these objects. The Visitor mode allows you to set related operations in a class.

3) when the object structure is shared by many applications, use the Visitor mode to allow each application to only include the operations needed.

4) classes that define the object structure are rarely changed, but new operations are often needed in this structure. Changing the object structure class requires redefining the interfaces for all visitors, which may be costly. If the object structure class changes frequently, it may be better to define these operations in these classes.


The comment Visitor mode indicates an operation that acts on each element in an object structure. It allows you to define and act on this without changing the element classes...

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.