PHP design pattern-mediation pattern

Source: Internet
Author: User
Tags in domain zend framework
We will introduce the mediation mode to you. the purpose of this mode is to encapsulate the interaction between a group of objects to prevent mutual interference between objects. the Mediator is on the colleaguate object) serves as an intermediate aggregation point. Colleague objects should be loosely coupled to avoid introducing the mediation mode. the purpose of this mode is to encapsulate the interaction between a group of objects and prevent mutual interference between objects, mediator acts as an intermediate aggregation point between colleagues and 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.

498) this. style. width = 498; "border =" 0 "/>
Target of mediators and colleagues

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.

 
 
  1. /**
  2. * AbstractColleague.
  3. */
  4. interface Filter
  5. {
  6. public function filter($value);
  7. }
  8. /**
  9. * Colleague. We decide in the implementation phase
  10. * that Colleagues should not know the next Colleague
  11. * in the chain, resorting to a Mediator to link them together.
  12. * This choice succesfully avoids a base abstract class
  13. * for Filters.
  14. * Remember that this is an example: it is not only
  15. * Chain of Responsibility that can be alternatively implemented
  16. * as a Mediator.
  17. */
  18. class TrimFilter implements Filter
  19. {
  20. public function filter($value)
  21. {
  22. return trim($value);
  23. }
  24. }
 
 
  1. /**
  2. * Colleague.
  3. */
  4. class NullFilter implements Filter
  5. {
  6. public function filter($value)
  7. {
  8. return $value ? $value : '';
  9. }
  10. }
  11. /**
  12. * Colleague.
  13. */
  14. class HtmlEntitiesFilter implements Filter
  15. {
  16. public function filter($value)
  17. {
  18. return htmlentities($value);
  19. }
  20. }
 
 
  1. /**
  2. * The Mediator. We avoid referencing it from ConcreteColleagues
  3. * And so the need for an interface. We leave the implementation
  4. * Of a bidirectional channel for the Observer pattern's example.
  5. * This class responsibility is to store the value and coordinate
  6. * Filters computation when they have to be applied to the value.
  7. * Filtering responsibilities are obviusly a concern
  8. * The Colleagues, which are Filter implementations.
  9. */
  10. Class InputElement
  11. {
  12. Protected: We will introduce 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 is on the colleaguate object) serves as an intermediate aggregation point.

    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.

    498) this. style. width = 498; "border =" 0 "/>
    Target of mediators and colleagues

    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.

         
         
    1. /**
    2. * AbstractColleague.
    3. */
    4. interface Filter
    5. {
    6. public function filter($value);
    7. }
    8. /**
    9. * Colleague. We decide in the implementation phase
    10. * that Colleagues should not know the next Colleague
    11. * in the chain, resorting to a Mediator to link them together.
    12. * This choice succesfully avoids a base abstract class
    13. * for Filters.
    14. * Remember that this is an example: it is not only
    15. * Chain of Responsibility that can be alternatively implemented
    16. * as a Mediator.
    17. */
    18. class TrimFilter implements Filter
    19. {
    20. public function filter($value)
    21. {
    22. return trim($value);
    23. }
    24. }
         
         
    1. /**
    2. * Colleague.
    3. */
    4. class NullFilter implements Filter
    5. {
    6. public function filter($value)
    7. {
    8. return $value ? $value : '';
    9. }
    10. }
    11. /**
    12. * Colleague.
    13. */
    14. class HtmlEntitiesFilter implements Filter
    15. {
    16. public function filter($value)
    17. {
    18. return htmlentities($value);
    19. }
    20. }
    ___FCKpd___2

    Original article name: Practical Php Patterns: Mediator author: Giorgio

    Source: http://giorgiosironi.blogspot.com/search/label/practical%20php%20patterns

    Filters;
  13. Protected: We will introduce 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 is on the colleaguate object) serves as an intermediate aggregation point.

    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.

    498) this. style. width = 498; "border =" 0 "/>
    Target of mediators and colleagues

    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.

          
          
    1. /**
    2. * AbstractColleague.
    3. */
    4. interface Filter
    5. {
    6. public function filter($value);
    7. }
    8. /**
    9. * Colleague. We decide in the implementation phase
    10. * that Colleagues should not know the next Colleague
    11. * in the chain, resorting to a Mediator to link them together.
    12. * This choice succesfully avoids a base abstract class
    13. * for Filters.
    14. * Remember that this is an example: it is not only
    15. * Chain of Responsibility that can be alternatively implemented
    16. * as a Mediator.
    17. */
    18. class TrimFilter implements Filter
    19. {
    20. public function filter($value)
    21. {
    22. return trim($value);
    23. }
    24. }
          
          
    1. /**
    2. * Colleague.
    3. */
    4. class NullFilter implements Filter
    5. {
    6. public function filter($value)
    7. {
    8. return $value ? $value : '';
    9. }
    10. }
    11. /**
    12. * Colleague.
    13. */
    14. class HtmlEntitiesFilter implements Filter
    15. {
    16. public function filter($value)
    17. {
    18. return htmlentities($value);
    19. }
    20. }
    ___FCKpd___2

    Original article name: Practical Php Patterns: Mediator author: Giorgio

    Source: http://giorgiosironi.blogspot.com/search/label/practical%20php%20patterns

    Value;
  14. Public function addFilter (Filter $ filter)
  15. {
  16. $ This->_ Filters [] = $ filter;
  17. Return $ this;
  18. }
  19. Public function setValue ($ value)
  20. {
  21. $ This->_ Value = $ this->_ Filter ($ value );
  22. }
  23. Protected function _ filter ($ value)
  24. {
  25. Foreach ($ this->_ Filters as $ filter ){
  26. $ Value = $ filter->Filter ($ value );
  27. }
  28. Return $ value;
  29. }
  30. Public function getValue ()
  31. {
  32. Return $ this->_ Value;
  33. }
  34. }
  35. $ Input = new InputElement ();
  36. $ Input->AddFilter (new NullFilter ())
  37. ->AddFilter (new TrimFilter ())
  38. ->AddFilter (new HtmlEntitiesFilter ());
  39. $ Input->SetValue ('you should use

    -

    Tags for your headings .');

  40. Echo $ input->GetValue (), "\ n ";

Original article name: Practical Php Patterns: Mediator author: Giorgio

Source: http://giorgiosironi.blogspot.com/search/label/practical%20php%20patterns

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.