Design Pattern (15th) intermediary pattern mediator (object behavior pattern)

Source: Internet
Author: User

Design Pattern (15th) intermediary pattern Mediator (object behavior pattern)

1. Overview

In the process of object-oriented software design and development, according to the "single responsibility principle", we should try to refine the object so that it is only responsible for or presents a single responsibility, distribute behaviors to objects.

A module or system may consist of many objects, and these objects may reference each other. In the worst case, every object knows all other objects, this undoubtedly complicate the relationship between objects. Although the division of a system into many objects can enhance reusability, the increase in inter-object connection will reduce reusability, A large number of interconnections make it impossible for an object to work without the support of other objects. The system is represented as an integral whole, it is also very difficult to make any major changes to system behavior. The result is that you have to define a large number of sub-classes to customize system behavior. Therefore, we need to use Intermediary mode. Example 1:

2. Problem

Face a series of interactive objects. How can we ensure that objects do not need to be explicitly referenced to each other to make them loosely coupled?

3. Solution

Intermediary mode: a mediation object is used to encapsulate a series of object interactions. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently. The intermediary mode is also called the intermediary mode. (Define
An object thatencapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects fromreferring to each other explicitly, and it
Lets you vary their interaction independently .)

4. Applicability

Use the intermediary mode in the following situations:

• 1) there are complex reference relationships between objects in the system, resulting in chaotic and difficult to understand the dependency structure.

• 2) a group of objects communicate in a well-defined but complex manner. The resulting dependency structure is confusing and hard to understand.
• 3) an object references many other objects and communicates directly with them, making it difficult to reuse the object.
• 4) You want to use an intermediate class to encapsulate the behavior of multiple classes without generating too many child classes. You can introduce the intermediary class to define the public behavior of object interaction in the intermediary. If you need to change the behavior, you can add a new intermediary class.

5. Structure

6. Composition of the Mode

Abstract Mediator: The Mediator defines an interface for communicating with colleagues' objects.
ConcreteMediator. Understand and maintain its colleagues.
Abstract Colleague class: defines Colleague class interfaces and public methods of colleagues.
ConcreteColleague: Implements methods in abstract colleagues' classes. At the same time, each class needs to know the intermediary object. Each specific colleague class only needs to understand its own behavior, rather than the situation of other colleagues' classes. When a colleague object needs to communicate with other colleagues, it communicates with its intermediary.

7. Effect

Advantages of intermediary mode:
1) Reduce subclass generation: Mediator aggregates the behavior originally distributed among multiple objects. To change these actions, you only need to generate a subclass of Mediator. In this way, various colleags can be reused.
2) simplify the design and implementation of various colleagues' classes: It decouples colleagues' classes, and Mediator facilitates loose coupling between colleagues' classes. you can independently change and reuse the collection class and Mediator class.
3) It simplifies the Object Protocol: the one-to-many interaction between the Mediator and Colleague replaces the many-to-many interaction. One-to-multiple relationships are easier to understand, maintain, and expand.
4) It abstracts how objects collaborate and uses mediation as an independent concept and encapsulates it in an object, this allows you to focus on the interaction between objects. This helps to figure out how objects interact in a system.
5) It enables centralized mediation control to change the complexity of interaction into the complexity of the intermediary.
Disadvantages of intermediary mode:
Because the intermediary encapsulates the protocol, that is, contains the interaction details between colleagues in a specific intermediary class, the specific intermediary class may be very complicated, this may make the intermediary itself difficult to maintain
Giant.

8. Implementation

<? Php/*** intermediary mode *** // *** abstract intermediary class */abstract class Mediator {static protected $ _ colleaguesend = array ('concretecolleague1 '=>' concreteColleague2 ', 'cretecolleague3 ', 'cretecolleague3' => 'cretecolleague1 ',); protected $ _ colleagues = null; // arraypublic 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]-> your Y ($ message);} return ;}} /*** abstract Colleague class */abstract class Colleague {protected $ _ mediator = null; public function _ construct ($ mediator) {$ this-> _ mediator = $ mediator; $ mediator-> register ($ this);}/*** call each other through an intermediary */public abstract function send ($ message ); /*** specific business logic code 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 running y ($ 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 Y ($ message) {echo 'concretecolleague2 m: ', $ message,' <br/> ';}} /*** 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 running y ($ message) {echo 'concretecolleague3 m: ', $ message,' <br/> ';}}$ objMediator = new ConcreteMediator (); $ objC1 = new ConcreteColleague1 ($ objMediator ); $ objC2 = new ConcreteColleague2 ($ objMediator); $ objC3 = new ConcreteColleague3 ($ objMediator); $ objC1-> send ("from ConcreteColleague1 "); $ objC2-> send ("from ConcreteColleague2"); $ objC3-> send ("from ConcreteColleague3 "); /*************************************** *************/

9. Other related models

1) Appearance mode. Facade differs from the intermediary in that it abstracts an object subsystem and provides a convenient interface. Its Protocol is unidirectional, that is, the Facade object initiates a request to this sub-system class, but the opposite is not. On the contrary, Mediator provides collaborative behaviors that are not supported or not supported by each Colleague object, and the protocol is multi-direction.
2) colleags can communicate with Mediator in Observers mode.

10. Summary and Analysis

1)A typical application of the dimit rule:In the intermediary mode, by creating an intermediary object,Minimizes the number of other objects referenced by related objects in the system.So that the interaction between an object and its colleagues is replaced by the interaction between the object and the intermediary object. Therefore,The intermediary mode is a typical application of the dimit rule..
2) by introducing the intermediary object, the system's mesh structure can be changed to a star structure centered on the intermediary, and the intermediary undertakes the transit and coordination roles. The intermediary class is the core of the intermediary mode. It controls and coordinates the entire system, simplifies interaction between objects, and further controls interaction between objects.
3) the main advantage of the intermediary mode is that it simplifies interaction between objects, decouples colleagues, and reduces subclass generation. For complex interaction between objects, it introduces the intermediary, it can simplify the design and implementation of various colleagues' classes. The main disadvantage of the intermediary mode is that the specific intermediary class contains the interaction details between colleagues, which may lead to complicated specific intermediary classes, this makes the system difficult to maintain.
4) The intermediary mode applies to complex reference relationships between objects in the system, resulting in chaotic and hard-to-understand dependency relationships; an object references many other objects and communicates directly with these objects, making it difficult to reuse the object. to encapsulate the behavior of multiple classes through an intermediate class, instead of generating too many child classes

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.