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?
- <?php
- /**
- * Broker Mode
- *
- *
- */
- /**
- * Abstract Intermediary class
- */
- Abstract class Mediator
- {
- static protected $_colleaguesend = Array (
- ' ConcreteColleague1 ' = ' ConcreteColleague2 ',
- ' ConcreteColleague2 ' = ' ConcreteColleague3 ',
- ' ConcreteColleague3 ' = ' ConcreteColleague1 ',
- );
- protected $_colleagues = null; //array
- Public Function Register (colleague $colleague) {
- $this->_colleagues[get_class ($colleague)] = $colleague;
- }
- Public abstract function operation ($name, $message);
- }
- /**
- * Specific Intermediary class
- */
- Class Concretemediator extends mediator
- {
- Public function operation ($obj, $message) {
- $className = self::$_colleaguesend[get_class ($obj)];
- if ($className = = Get_class ($obj)) {
- return;
- }
- if ($this->_colleagues[$className]) {
- $this->_colleagues[$className]->notify ($message);
- }
- return;
- }
- }
- /**
- * Abstract Colleague Class
- */
- Abstract class colleague
- {
- protected $_mediator = null;
- Public function __construct ($mediator) {
- $this->_mediator = $mediator;
- $mediator->register ($this);
- }
- /**
- * Mutual invocation through mediations
- */
- Public abstract function Send ($message);
- /**
- * Specific business logic code that needs to be implemented
- */
- Public Abstract function notify ($message);
- }
- /**
- * Specific colleague Class
- */
- Class ConcreteColleague1 extends colleague
- {
- Public function __construct (mediator $mediator) {
- Parent::__construct ($mediator);
- }
- Public function Send ($message) {
- $this->_mediator->operation ($this, $message);
- }
- Public function notify ($message) {
- echo ' ConcreteColleague1 m: ', $message, ' <br/> ';
- }
- }
- /**
- * Specific colleague Class
- */
- Class ConcreteColleague2 extends colleague
- {
- Public function __construct (mediator $mediator) {
- Parent::__construct ($mediator);
- }
- Public function Send ($message) {
- $this->_mediator->operation ($this, $message);
- }
- Public function notify ($message) {
- echo ' ConcreteColleague2 m: ', $message, ' <br/> ';
- }
- }
- /**
- * Specific colleague Class
- */
- Class ConcreteColleague3 extends colleague
- {
- Public function __construct (mediator $mediator) {
- Parent::__construct ($mediator);
- }
- Public function Send ($message) {
- $this->_mediator->operation ($this, $message);
- }
- Public function notify ($message) {
- echo ' ConcreteColleague3 m: ', $message, ' <br/> ';
- }
- }
- $objMediator = new Concretemediator ();
- $objC 1 = new ConcreteColleague1 ($objMediator);
- $objC 2 = new ConcreteColleague2 ($objMediator);
- $objC 3 = new ConcreteColleague3 ($objMediator);
- $objC 1->send ("from ConcreteColleague1");
- $objC 2->send ("from ConcreteColleague2");
- $objC 3->send ("from ConcreteColleague3");
- /****************************************************/
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)