Home Backend Development PHP Tutorial An in-depth analysis of the mediator pattern in PHP design patterns_PHP Tutorial

An in-depth analysis of the mediator pattern in PHP design patterns_PHP Tutorial

Jul 21, 2016 pm 03:07 PM
php right encapsulation yes model go deep of Purpose parse Design Patterns this

Mediator pattern, the purpose of this pattern is to encapsulate the interaction between a group of objects and prevent the objects from interfering with each other. The Mediator (Mediator) acts as an intermediate convergence point between Colleague objects (Colleague). Colleague objects should be loosely coupled to avoid one object pointing directly to another object. In the mediator mode, when the relationships and dependencies of objects conflict, we can use the mediator to coordinate the workflow between coupled objects. Dependencies can be established from colleagues to the mediator or from the mediator to colleagues in both directions. Dependencies can be broken using AbstractColleague or AbstractMediator.



Objects are not isolated, they must cooperate with each other to complete tasks. Although the mediator pattern can limit the interaction between objects, if abused, it can make writing aggregate classes very difficult. To give a practical example, services in Domain-Driven Design are mediators between entities. To give another PHP-related example, the Zend_Form decoration and filtering functions can actually be seen as a simple mediator between Zend_Form_Decorator and Zend_Filter instances, both of which use Zend_Validate objects for validation.

When the mediator must listen to events on colleague objects, it is usually implemented as an Observer, producing a blackboard object for some colleagues to write and others to read. Events from colleagues are pushed to the mediator, who then forwards them to other subscribed colleagues. Colleagues do not need to know each other. This architecture is successfully used in the Dojo JavaScript library released with the Zend Framework. Another benefit of this pattern is that the changes to the object are contained in the calculation method. This can be achieved by configuring different mediators, but instantiating related objects will be a loose operation, and the collaborative relationship between different containers and factories will is decentralized.

Participants:
◆Colleague: The point is its responsibilities, it only communicates with one mediator or AbstractMediator.
◆Mediator: Work together with multiple Colleagues (AbstractColleagues).
◆AbstractMediator, AbstractColleague: Optional interfaces decoupled from the real implementation of these roles, there may be more than one AbstractColleague role.
The following code implements a form input filtering process, similar to the Zend_Form_Element function.

Copy code The code is as follows:

    <?php
    /** 
     * AbstractColleague. 
    */ 
    interface Filter 
    { 
 public function filter($value); 
    } 

    /** 
     * Colleague. We decide in the implementation phase 
     * that Colleagues should not know the next Colleague 
     * in the chain, resorting to a Mediator to link them together. 
     * This choice succesfully avoids a base abstract class 
     * for Filters. 
     * Remember that this is an example: it is not only 
     * Chain of Responsibility that can be alternatively implemented 
     * as a Mediator. 
    */ 
    class TrimFilter implements Filter 
    { 
  public function filter($value) 
  { 
      return trim($value); 
  } 
    } <PRE class=php name="code">    /** 
     * Colleague. 
    */ 
    class NullFilter implements Filter 
    { 
 public function filter($value) 
 { 
     return $value ? $value : ''; 
 } 
    } 

    /** 
     * Colleague. 
    */ 
    class HtmlEntitiesFilter implements Filter 
    { 
 public function filter($value) 
 { 
     return htmlentities($value); 
 } 
    }
</PRE><PRE class=php name="code">    /** 
     * The Mediator. We avoid referencing it from ConcreteColleagues 
     * and so the need for an interface. We leave the implementation 
     * of a bidirectional channel for the Observer pattern's example. 
     * This class responsibility is to store the value and coordinate 
     * filters computation when they have to be applied to the value. 
     * Filtering responsibilities are obviously a concern of 
     * the Colleagues, which are Filter implementations. 
    */ 
    class InputElement 
    { 
 protected $_filters; 
 protected $_value; 

 public function addFilter(Filter $filter) 
 { 
     $this->_filters[] = $filter; 
     return $this; 
 } 

 public function setValue($value) 
 { 
     $this->_value = $this->_filter($value); 
 } 

 protected function _filter($value) 
 { 
     foreach ($this->_filters as $filter) { 
  $value = $filter->filter($value); 
     } 
     return $value; 
 } 

 public function getValue() 
 { 
     return $this->_value; 
 }   
    } 

    $input = new InputElement(); 
    $input->addFilter(new NullFilter()) 
   ->addFilter(new TrimFilter()) 
   ->addFilter(new HtmlEntitiesFilter()); 
    $input->setValue(' You should use the <h1>-<h6> tags for your headings.'); 
    echo $input->getValue(), "n";
</PRE>
<PRE></PRE>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327553.htmlTechArticleMediator pattern, the purpose of this pattern is to encapsulate the interaction between a group of objects and prevent the objects from interacting with each other. Interference, the mediator (Mediator) acts between colleague objects (Colleague)...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles