Mediator (design sample)
A mediator ( English mediator pattern ) is in the software development a design pattern , the (English to the category of behavior pattern behavioral patterns is one). The pattern is used to control the cooperative behavior of objects, whereby objects do not cooperate directly, but via an intermediary. It is one of the so-called GoF patterns (see Gang of Four ) .
The mediator applies if
- Objects in a system cooperate in a complex way,
- the reuse of objects is made difficult by the reference to many other objects,
- or objects cannot or should not know other objects with which they cooperate.
The mediator defines the interface for communication with colleagues. The concrete mediator implements the cooperative behavior by coordinating the colleagues involved. He knows and manages colleagues involved. The individual colleagues know their intermediary and communicate with him instead of with other colleagues.
The coordination of the cooperative behavior is administered centrally. A change in cooperative behavior can be implemented independently of the colleagues involved. Such changes in behavior can be achieved through new concrete intermediaries. This restricts the formation of subclasses, since the changes are not made in the various specific colleagues and accordingly many new subclasses have to be created. The pattern supports a loose coupling between colleagues. The minutes of colleagues are simplified.
Since the mediator encapsulates behavior that would otherwise be distributed across multiple classes, it is itself more complex than the individual components would have been. There is a risk that a monolithic program complex will arise that is difficult to maintain and expand.
A typical example of an intermediary is a chat room . Chatters log in and out of the chat room in order to communicate with other chatters. They do not communicate directly with each other, but via the chat room. Different specific chat rooms can now enable different types of communication. A chat room can forward messages from a chatter to all chatters without the chatter having to know them. Another chat room, however, can only enable communication between individual chatters. If you want to add logging to communication, you can expand a specific chat room while the chatters are not affected.
Related design patterns
Colleagues can watch the agent and vice versa.