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)