Design mode (15) Mediator mode Mediator (object behavior type)

Source: Internet
Author: User

Design mode (15) Mediator mode Mediator (object behavior type)

1. Overview

In object-oriented software design and development process, according to the "single responsibility Principle", we should try to refine the object, so that it is only responsible for or present a single responsibility, the behavior of the distribution into the various objects.

For a module or system, it may consist of many objects, and there may be mutual references between these objects, and in the worst case, every object knows all other objects, which complicates the connection between the objects. Although splitting a system into many objects can often enhance reusability, the proliferation of inter-object connections reduces its reusability, and a large number of interconnected objects seem unlikely to work without the support of other objects, and the system behaves as an indivisible whole. And any major changes to the system's behavior can be very difficult. The result is that you have to define a large number of subclasses to customize the behavior of the system. Therefore, in order to reduce the complex reference relationship between object 22 and make it a loosely coupled system, we need to use the mediator pattern. Example 1:

2. Questions

Face a series of intersecting objects. How do you ensure that each object does not need to explicitly cross-reference each other so that it is loosely coupled?

3. Solution

Mediator mode: A Mediation object is used to encapsulate a series of object interactions. The mediator makes the objects not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them. The mediator pattern is also known as the mediator pattern. (Define an object thatencapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects fromreferring to all other explicitly, and it lets you vary their int Eraction independently. )

4. Applicability

Use the mediator mode in the following situations:

1) There are complex referential relationships between objects in the system, and the resulting interdependencies are confusing and difficult to understand. ·2) A set of objects communicates in a well-defined but complex way. The resulting interdependence structure is confusing and difficult to understand.
·3) An object that references many other objects and communicates directly with those objects makes it difficult to reuse the object.
4) wants to encapsulate the behavior in multiple classes through an intermediate class, without having to generate too many subclasses. It can be implemented by introducing a mediator class that defines the public behavior of the object interaction in the mediator and adds a new mediator class if the behavior needs to be changed.

5. Structure

6. Composition of the pattern

Abstract Mediator (mediator): A mediator defines an interface for communicating with each colleague (colleague) object.
Specific intermediary (Concretemediator): The specific intermediary through the coordination of the colleagues to achieve collaborative behavior. Understand and maintain all of its colleagues.
Abstract colleague Class (colleague Class): Defines a colleague class interface that defines the public methods of each colleague.
Specific colleague Class (Concretecolleague): Implement methods in the abstract colleague class. Each concurrent class needs to know the intermediary object, and each individual colleague class only needs to know its own behavior, without needing to know the situation of other colleague classes. Each colleague object communicates with its intermediary when it needs to communicate with other colleagues.

7. Effects

Advantages of the broker mode:
1) reduced subclass generation: Mediator The behavior that was originally distributed across multiple objects. Changing these behaviors requires only the generation of mediator subclasses. This allows the various colleague classes to be reused.
2) Simplify the design and implementation of each colleague class: it colleague each colleague class and mediator facilitates the loose coupling between the colleague. You can change and reuse each colleague class and the Mediator class independently.
3) It simplifies the object protocol: replaces many-to-many interactions with a one-to-many interaction between the mediator and the colleague. A one-to-many relationship is easier to understand, maintain, and extend.
4) It abstracts how objects work together and encapsulates the mediation as a separate concept into an object, allowing you to shift your attention from the behavior of the objects themselves to the interaction between them. This helps to figure out how the objects in a system interact.
5) It makes the control centralization of the mediator pattern the complexity of the interaction into the mediator's complexity.
Disadvantages of the broker pattern:
Because intermediaries encapsulate the protocol, which includes the interaction details between colleagues in a specific mediator class, it can lead to a very complex class of intermediaries, which may make the mediator itself a difficult to maintain
Behemoth.

8. Implement

[PHP]View Plain Copy print?
  1. <?php
  2. /**
  3. * Broker Mode
  4. *
  5. *
  6. */
  7. /**
  8. * Abstract Intermediary class
  9. */
  10. Abstract class Mediator
  11. {
  12. static protected $_colleaguesend = Array (
  13. ' ConcreteColleague1 ' = ' ConcreteColleague2 ',
  14. ' ConcreteColleague2 ' = ' ConcreteColleague3 ',
  15. ' ConcreteColleague3 ' = ' ConcreteColleague1 ',
  16. );
  17. protected $_colleagues = null; //array
  18. Public Function Register (colleague $colleague) {
  19. $this->_colleagues[get_class ($colleague)] = $colleague;
  20. }
  21. Public abstract function operation ($name, $message);
  22. }
  23. /**
  24. * Specific Intermediary class
  25. */
  26. Class Concretemediator extends mediator
  27. {
  28. Public function operation ($obj, $message) {
  29. $className = self::$_colleaguesend[get_class ($obj)];
  30. if ($className = = Get_class ($obj)) {
  31. return;
  32. }
  33. if ($this->_colleagues[$className]) {
  34. $this->_colleagues[$className]->notify ($message);
  35. }
  36. return;
  37. }
  38. }
  39. /**
  40. * Abstract Colleague Class
  41. */
  42. Abstract class colleague
  43. {
  44. protected $_mediator = null;
  45. Public function __construct ($mediator) {
  46. $this->_mediator = $mediator;
  47. $mediator->register ($this);
  48. }
  49. /** 
  50. * Mutual invocation through mediations
  51. */
  52. Public abstract function Send ($message);
  53. /** 
  54. * Specific business logic code that needs to be implemented
  55. */
  56. Public Abstract function notify ($message);
  57. }
  58. /**
  59. * Specific colleague Class
  60. */
  61. Class ConcreteColleague1 extends colleague
  62. {
  63. Public function __construct (mediator $mediator) {
  64. Parent::__construct ($mediator);
  65. }
  66. Public function Send ($message) {
  67. $this->_mediator->operation ($this, $message);
  68. }
  69. Public function notify ($message) {
  70. echo ' ConcreteColleague1 m: ', $message, ' <br/> ';
  71. }
  72. }
  73. /**
  74. * Specific colleague Class
  75. */
  76. Class ConcreteColleague2 extends colleague
  77. {
  78. Public function __construct (mediator $mediator) {
  79. Parent::__construct ($mediator);
  80. }
  81. Public function Send ($message) {
  82. $this->_mediator->operation ($this, $message);
  83. }
  84. Public function notify ($message) {
  85. echo ' ConcreteColleague2 m: ', $message, ' <br/> ';
  86. }
  87. }
  88. /**
  89. * Specific colleague Class
  90. */
  91. Class ConcreteColleague3 extends colleague
  92. {
  93. Public function __construct (mediator $mediator) {
  94. Parent::__construct ($mediator);
  95. }
  96. Public function Send ($message) {
  97. $this->_mediator->operation ($this, $message);
  98. }
  99. Public function notify ($message) {
  100. echo ' ConcreteColleague3 m: ', $message, ' <br/> ';
  101. }
  102. }
  103. $objMediator = new Concretemediator ();
  104. $objC 1 = new ConcreteColleague1 ($objMediator);
  105. $objC 2 = new ConcreteColleague2 ($objMediator);
  106. $objC 3 = new ConcreteColleague3 ($objMediator);
  107. $objC 1->send ("from ConcreteColleague1");
  108. $objC 2->send ("from ConcreteColleague2");
  109. $objC 3->send ("from ConcreteColleague3");
  110. /****************************************************/


9. With other related modes

1) Appearance mode, facade differs from the mediator in that it is an abstraction of an object subsystem, thus providing a convenient interface. Its protocol is unidirectional, that is, the facade object requests the subsystem class, but vice versa. Instead, mediator provides collaborative behavior that is not supported or supported by each colleague object, and the protocol is multidirectional.
2) colleague can communicate with mediator using observers mode.

10. Summary and Analysis

1) A typical application of the Dimitri rule: in mediator mode, by creating a mediator object, the number of other objects referenced in the system is reduced to a minimum , The interaction between an object and its colleagues is replaced by the interaction between the object and the Mediator object. Therefore, the mediator pattern is a typical application of the Dimitri Law .
2) by introducing the intermediary object, the network structure of the system can be transformed into a star-shaped structure centered on the intermediary, and the intermediary takes the role of relay and coordination. The Mediator class is the core of the mediator pattern, which controls and coordinates the whole system, simplifies the interaction between objects, and can further control the interaction between objects.
3 The main advantage of the mediator mode is that it simplifies the interaction between the objects, decouples the colleagues, and reduces the generation of subclasses, and the interaction between complex objects, by introducing intermediaries, can simplify the design and implementation of each colleague class. The main disadvantage of the mediator pattern is that the specific mediator class contains the interaction details between colleagues. May lead to a very complex class of intermediaries, making the system difficult to maintain.
4) The application of the mediator pattern includes: There are complex referential relationships between objects in the system, and the resulting interdependencies are confusing and difficult to understand; an object is difficult to reuse because it refers to many other objects and communicates directly with these objects; you want to encapsulate the behavior in multiple classes through an intermediate class. And you don't want to generate too many subclasses.

Top
0

Design mode (15) Mediator mode Mediator (object behavior type)

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.