Broker Mode (mediator)

Source: Internet
Author: User

1. Definition

Encapsulates a series of object interactions with a mediation object. 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.

2. Structure

Mediator: Broker interface. A way to define the interactions between colleagues, which can be a public communication method

Concretemediator: The specific mediator implements the object. It is responsible for understanding and maintaining the individual colleague objects, and is responsible for the specific coordination of the interaction between the colleague objects

Colleague: the definition of a colleague class, typically implemented as an abstract class, is primarily responsible for constraining the types of colleague objects and implementing public methods between specific colleague classes.

Concretecolleague: The specific colleague class, realizes own business, when needs to communicate with the other colleague, communicates with the holding intermediary, the intermediary will be responsible for interacts with other colleague.

Coding ...

3. Generalized intermediaries

Is it necessary to define a common parent class for a colleague object?

Is it necessary for a colleague class to hold an intermediary object?

Do you need a mediator interface?

Does the intermediary object need to hold all the colleagues?

Does the mediator object simply provide a public way to accept notifications from colleague objects?

4. Nature

Encapsulating interactions

5. Select the broker mode

If the communication between a group of objects is more complex, resulting in mutual dependence, structure confusion, you can use the intermediary mode, the interaction of these objects with each other management, each object only need to interact with the mediator, so that the objects are loosely coupled, the structure is more clear and understandable

If an object references multiple objects, the disease interacts with these objects, which makes it difficult to take the object and can be used in the mediator mode.

6. Related modes

Appearance mode

Observer pattern

Understand

When there are multiple business classes interacting with each other, you can consider the intermediary mode, the general intermediary pattern can be considered as a singleton mode, so that the corresponding notification method can be called directly in different business notifications. For example, a write data, notify B write, b write data, notify C write, c after writing the data to notify a, the mutual invocation of the coupling logic all to the intermediary, right intermediary to coordinate the implementation.

Broker Mode (mediator)

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.