Learning php design pattern php Visitor pattern (Visitor)

Source: Internet
Author: User
Tags php example
This article mainly introduces the visitor mode in the php design mode and uses php to implement the visitor mode. interested friends can refer to the visitor mode to indicate an operation that acts on each element of an object structure. It can define new operations acting on these elements without modifying each element class, that is, dynamically adding a specific Visitor role.
The visitor mode uses double dispatch. First, pass the visitor to the Accept method of the element object, and then pass the element object into the visitor. then, the visitor executes the corresponding method of the element.
The visitor mode is generally used in the case of diverse aggregation types. In the normal form, you must determine the type of each element and perform corresponding operations to generate a lengthy conditional transfer statement. The visitor mode can better solve this problem. You can call $ element-> accept ($ vistor) for each element.
The visitor mode is mostly used when the structure of the Accessed class is relatively stable, that is, no subclass is added. The visitor mode allows the access structure to add new methods.
The Visitor mode separates the elements in the object structure from the operations on these elements, so that when we call methods based on the elements in the object structure, you do not need to use the IF statement to determine, that is, encapsulate the operation.
However, adding new element nodes will lead to changes including the visitor interface and its subclass, which violates Principle of opening/closing.
When this happens, it generally means that the visitor mode may no longer be applicable, or there is a problem with the design!
1. structure of the Visitor mode

II. main roles in Visitor mode

1) abstract Visitor role (Visitor ):Provides an access operation interface for each specific element in the ObjectStructure. The operation interface name and parameters identify the specific element role to be accessed. In this way, the visitor can directly access the element through a specific interface of the element role.
2) specific Visitor role (ConcreteVisitor ):Implements the operations on role declarations for specific elements in the abstract Visitor role interface.
3) abstract Node role:This interface defines an accept operation to accept specific visitors.
4) specific Node roles:Implements the accept operation in the abstract node role.
5) ObjectStructure ):This is a necessary role to use the visitor mode. It must have the following features: it can enumerate its elements; it can provide a high-level interface to allow the visitor to access its elements; it can be a combination (combination mode) or a set, for example, a list or an unordered set (in PHP, we use arrays instead, because arrays in PHP are originally a set that can hold any type of data)
III. Advantages and disadvantages of the Visitor mode
The visitor mode has the followingAdvantages:
1) The Visitor mode makes it easy to add new operations. The visitor mode allows you to add new operations without modifying specific element classes. It is implemented mainly through the accept method of the element class to accept a new visitor object. If some operations depend on a complex structure object, it is generally complicated to add new operations. In the visitor mode, adding new operations means adding a new visitor class. Therefore, it becomes easy.
2) The Visitor mode aggregates relevant behaviors into a visitor object, instead of being dispersed into node classes.
3) the visitor mode can access member classes of different hierarchies through hierarchical structures of several classes. An iterator can only access member objects of the same type of hierarchy, but cannot access objects of different levels. The visitor mode can achieve this.
4) accumulation status. Each individual visitor object has a concentrated set of related behaviors, so that the status of the executed operations can be accumulated within itself during the access process, rather than distributed to many node objects. This is beneficial to system maintenance.

The visitor mode has the followingDisadvantages:
1) it is very difficult to add new node classes. Adding a new node means adding a new abstract operation to the abstract Visitor role and adding corresponding operations to each specific visitor class.
2) destroys encapsulation. The visitor mode requires the visitor object to access and call operations on each node object, which implies a requirement on all node objects: they must expose some of their own operations and internal states. Otherwise, the visitor's access becomes meaningless. Because the visitor object accumulates the states required for access operations, the states are no longer stored in the node object, which also destroys encapsulation.

Prerequisite for using the Visitor mode: the object type in the object Group structure (Collection) rarely changes.
In the Visitor and Element interfaces, ensure that the elements are rarely changed. that is, ensure that new Element types cannot be added frequently. What changes can be Visitor behavior or operation, that is, different sub-classes of Visitor can have multiple types, which makes it easier to use the Visitor mode.
If the object set in the object set changes frequently, not only the Visitor implementation needs to change, but also the ConcreteVisitor needs to increase the corresponding behavior. GOF recommends that you define operations one by one in these object classes, the visitor design mode is not required.

IV. Visitor mode and other modes

1. if the structure object browsed is linear, it is also possible to use the iteration mode instead of the visitor mode.
2. view some structure objects in the synthesis mode in the visitor mode.
The above two points are from the book Java and patterns.

V. Visitor PHP example

<? Php interface Visitor {public function visitConcreteElementA (ConcreteElementA $ elementA); public function visitConcreteElementB (concreteElementB $ elementB);} interface Element {public function accept (Visitor $ visitor );} /*** specific Visitor 1 */class ConcreteVisitor1 implements Visitor {public function visitConcreteElementA (ConcreteElementA $ elementA) {echo $ elementA-> getName (). "visitd by ConcerteVisitor1
";} Public function visitConcreteElementB (ConcreteElementB $ elementB) {echo $ elementB-> getName ()." visited by ConcerteVisitor1
";}}/*** Specific Visitor 2 */class ConcreteVisitor2 implements Visitor {public function visitConcreteElementA (ConcreteElementA $ elementA) {echo $ elementA-> getName (). "visitd by ConcerteVisitor2
";} Public function visitConcreteElementB (ConcreteElementB $ elementB) {echo $ elementB-> getName ()." visited by ConcerteVisitor2
";}}/*** Specific Element A */class ConcreteElementA implements Element {private $ _ name; public function _ construct ($ name) {$ this-> _ name = $ name;} public function getName () {return $ this-> _ name ;} /*** accept the Visitor to call its new method for this element * @ param visitor $ Visitor */public function accept (visitor $ Visitor) {$ visitor-> visitConcreteElementA ($ this) ;}}/*** specific Element B */class ConcreteElementB implements Element {private $ _ Name; public function _ construct ($ name) {$ this-> _ name = $ name;} public function getName () {return $ this-> _ name ;} /*** accept the Visitor to call its new method for this element * @ param visitor $ Visitor */public function accept (visitor $ Visitor) {$ visitor-> visitConcreteElementB ($ this) ;}/ *** object structure is the set of elements */class ObjectStructure {private $ _ collection; public function _ construct () {$ this-> _ collection = array ();} public funct Ion attach (Element $ element) {return array_push ($ this-> _ collection, $ element);} public function detach (Element $ element) {$ index = array_search ($ element, $ this-> _ collection); if ($ index! = FALSE) {unset ($ this-> _ collection [$ index]);} return $ index;} public function accept (Visitor $ visitor) {foreach ($ this-> _ collection as $ element) {$ element-> accept ($ visitor) ;}} class Client {/*** Main program. */public static function main () {$ elementA = new ConcreteElementA ("ElementA"); $ elementB = new ConcreteElementB ("ElementB "); $ elementA2 = new ConcreteElementB ("ElementA2"); $ visit Or1 = new ConcreteVisitor1 (); $ visitor2 = new ConcreteVisitor2 (); $ OS = new ObjectStructure (); $ OS-> attach ($ elementA ); $ OS-> attach ($ elementB); $ OS-> attach ($ elementA2); $ OS-> detach ($ elementA); $ OS-> accept ($ visitor1 ); $ OS-> accept ($ visitor2) ;}} Client: main ();?>

The above is the code that uses php to implement the visitor mode. There are also some concepts about the visitor mode, and I hope it will be helpful for everyone's learning.

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.