PHP design mode-visitor mode

Source: Internet
Author: User

A visitor pattern is a behavioral pattern in which a visitor represents an action that acts on the elements of an object's structure. It can define new operations that act on these elements without modifying the individual element classes, i.e. dynamically increasing the role of the specific visitor.

The visitor pattern takes advantage of double allocation. The visitor first passes in the accept method of the Element object, and then the element object then passes itself to the visitor, and then the visitor executes the appropriate method of the element.

Main role

Abstract visitor Role (Visitor): Provides an access operation interface for each specific element in the object structure (objectstructure). The name and parameters of the operation interface identify the specific element role to access. This allows the visitor to access it directly through the specific interface of the element's role.
Specific visitor Role (Concretevisitor): Implements the actions of the abstract visitor role interface for each specific element role declaration.
Abstract node Role: This interface defines an accept operation that accepts a specific visitor.
Node role: Implements the accept operation in the abstract node role.
Object structure Role (Objectstructure): This is an essential role for using the visitor pattern. It has the following characteristics: it can enumerate its elements, can provide a high-level interface to allow the visitor to access its elements, can be a composite (composite mode) or a collection, such as a list or an unordered collection (in PHP we use arrays instead, Because an array in PHP is a set of data that can be placed in any type.
Applicability

Visitor patterns are used in a variety of aggregation types. In the normal form, it is necessary to judge what type each element belongs to and then proceed accordingly, thus creating a lengthy conditional transfer statement. And the visitor pattern can be a better solution to this problem. Unified call to each element element−>acC epT(vistor).
When the visitor pattern is more stable for the class structure being accessed, the subclass is not arbitrarily added. The visitor pattern allows new methods to be added to the structure being accessed.

<?phpinterface Visitor {//Abstract visitor role Public Function Visitconcreteelementa (Concreteelementa $elementA); Public Function Visitconcreteelementb (Concreteelementb $elementB);} Interface Element {//Abstract node role public function accept (Visitor $visitor);} Class ConcreteVisitor1 implements Visitor {///specific Visitors 1 public function Visitconcreteelementa (Concreteelementa $elementA {} public Function Visitconcreteelementb (Concreteelementb $elementB) {}}class ConcreteVisitor2 implements Visitor {/ /Specific Visitor 2 public function Visitconcreteelementa (Concreteelementa $elementA) {} public Function Visitconcreteelementb (Concreteelementb $elementB)    {}}class Concreteelementa implements element {//specific element a private $_name;     Public function __construct ($name) {$this->_name = $name;}    Public Function GetName () {return $this->_name;}    Public function Accept (Visitor $visitor) {//accepts that the visitor invokes its new method for the element $visitor->visitconcreteelementa ($this); }}class CONCRETEELEMENTB ImplemEnts element {//specific elements b private $_name;    Public function __construct ($name) {$this->_name = $name;}    Public Function GetName () {return $this->_name;}    Public function Accept (Visitor $visitor) {//accepts that the visitor invokes its new method for the element $visitor->visitconcreteelementb ($this);     }}class Objectstructure {//object structure is the collection of elements private $_collection;     Public Function __construct () {$this->_collection = array ();}    Public function Attach (Element $element) {return Array_push ($this->_collection, $element);        The Public function detach (Element $element) {$index = Array_search ($element, $this->_collection);        if ($index!== FALSE) {unset ($this->_collection[$index]);    } return $index; The public function accept (Visitor $visitor) {foreach ($this->_collection as $element) {$element-&        Gt;accept ($visitor); }}}//Client$elementa = new Concreteelementa ("Elementa"); $elementB = new ConcreteelementB ("Elementb"); $elementA 2 = new Concreteelementb ("ElementA2"); $visitor 1 = new ConcreteVisitor1 (); $visitor 2 = new ConcreteVisitor2 (); $os = new Objectstructure (); $os->attach ($elementA); $os->attach ($elementB); $os->attach ($elementA 2); $os->detach ($elementA), $os->accept ($visitor 1), $os->accept ($visitor 2);

PHP design mode-visitor mode

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.