In-depth analysis of the mediation mode in PHP design mode

Source: Internet
Author: User
Tags in domain zend framework

The mediation mode. The purpose of this mode is to encapsulate the interaction between a group of objects to prevent mutual interference between objects. The Mediator acts as an intermediate aggregation point between Colleague objects. Colleagues' objects should be loosely coupled to avoid an object pointing to another object explicitly. In the mediation by mode, when the relationship and dependency between objects conflict, we can use the mediation to coordinate the workflow between the coupled objects. The dependency can be established from a colleague to the mediation or from the mediation to the colleague, the dependency in both directions can be interrupted using abstractcolleags or AbstractMediator.

Objects are not isolated and must collaborate with each other to complete the task. Although the mediation mode can restrict the interaction between objects, it will become very difficult to compile the aggregated classes if abuse occurs. For example, the service in Domain-Driven Design is the mediation between entities. For another PHP-related example, the Zend_Form decoration and filtering functions can be regarded as a simple mediation between Zend_Form_Decorator and Zend_Filter instances. They all use the Zend_Validate object for verification.

When the mediator must listen to the events of a colleague object, it is usually implemented as an Observer to generate a blackboard object, written by some colleagues, and read by others. Events from colleagues are pushed to the mediation staff and then forwarded to other subscribe colleagues. colleagues do not need to know each other. This architecture is successfully used for the Dojo JavaScript Library released with the Zend framework. Another advantage of this mode is that the changes of objects are included in the calculation method. You can configure different mediators to achieve this goal, but instantiating the objects will be a loose operation, the relationships between containers and factories are scattered.

Participants:
◆ Collegou: it focuses on its responsibilities. It only communicates with one Mediator or AbstractMediator.
◆ Mediator: Works with multiple Colleagues.
◆ AbstractMediator and AbstractColleague: an optional interface for real decoupling of these roles. There may be more than one role AbstractColleague.
The following code implements a form input filtering process, similar to the Zend_Form_Element function.

Copy codeThe Code is as follows: <? Php
/**
* Maid.
*/
Interface Filter
{
Public function filter ($ value );
}

/**
* Colleague. We decide in the implementation phase
* That Colleagues shocould not know the next collegou
* 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 obviusly a concern
* 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 Echo $ input-> getValue (), "\ n ";
</PRE>
<PRE> </PRE>

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.