Mediator Pattern
The Mediator Pattern is used to reduce the communication complexity between multiple objects and classes, and it is a behavioral pattern.
The Mediator Pattern defines a mediator object to encapsulate the interactions between a series of objects. The mediator enables objects to avoid explicit mutual references, thereby loosening their coupling and allowing the interactions between them to be changed independently.
Introduction
intent
By introducing a mediator object to encapsulate and coordinate the interactions between multiple objects, the coupling between objects is reduced.
Main problems solved
- It solves complex one-to-many association problems between objects, avoids high coupling between objects, and simplifies the system structure.
Use Case
- When multiple classes in the system are coupled with each other, forming a network structure.
Implementation Approach
- Define the mediator interface: Specifies the interface that the mediator must implement.
- Create the concrete mediator: Implements the mediator interface, containing the logic for coordinating the interactions between colleague objects.
- Define Colleague Class: Each object no longer needs to explicitly reference one another, but interacts through the mediator.
Key code
- Mediator: Encapsulates the interaction logic between objects.
- Colleague classes: Communicate through the mediator.
Application example
- WTO: After China joined the WTO, countries trade through the WTO, which simplifies bilateral relations.
- Airport dispatch system: Coordinates aircraft takeoffs and landings, runway usage, etc.
- MVC Framework: The controller acts as the mediator between the model and the view.
Advantages
- Reduce Complexity: Converts the one-to-many relationship between multiple objects into a one-to-one relationship.
- Decoupling: Objects no longer directly reference one another; they interact through the mediator.
- Conforms to the Law of Demeter: Objects only need to know the mediator, not other objects.
Disadvantages
- Mediator complexity: The mediator may become huge and complex, making it difficult to maintain.
Usage suggestions
- When there are complex reference relationships between objects in the system, consider using the Mediator Pattern.
- Encapsulate the behavior of multiple classes through the mediator to avoid generating too many subclasses.
Notes
- Avoid using the Mediator Pattern when responsibilities are unclear or chaotic, as this may cause the mediator to assume too many responsibilities.
Structure
The Mediator Pattern includes the following main roles:
Mediator: Defines an interface for communicating with each colleague object and manages the relationships between colleague objects. It usually includes one or more event handling methods for processing various interaction events.
Concrete Mediator: Implements the mediator interface and is responsible for implementing the communication logic between colleague objects. It maintains references to each colleague object and coordinates their interactions.
Colleague objects (Colleague): Defines an interface for communicating with the mediator. It usually includes a method for sending messages and a method for receiving messages.
Concrete Colleague: Implements the colleague object interface and is the object that truly participates in the interaction. It sends its messages to the mediator, which forwards them to other colleague objects.
Implementation
We will demonstrate the Mediator pattern through a chat room example. In this example, multiple users can send messages to the chat room, and the chat room displays messages to all users. We will create two classesChatRoomandUser。UserObject UsageChatRoommethods to share their messages.
MediatorPatternDemo, our demonstration class usesUserobjects to display the communication between them.
Step 1
Create the mediator class.
ChatRoom.java
Step 2
Create the User class.
User.java
Step 3
UsageUserobjects to display the communication between them.
MediatorPatternDemo.java
Step 4
Run the program, output the result:
Thu Jan 31 16:05:46 IST 2013 [Robert] : Hi! John! Thu Jan 31 16:05:46 IST 2013 [John] : Hello! Robert!other extensions