Mediator Pattern
The Mediator Pattern (Mediator Pattern) is used to reduce the communication complexity between multiple objects and classes; it belongs to behavioral patterns.
The Mediator Pattern defines a mediator object to encapsulate the interactions between a series of objects. The mediator allows objects to not explicitly reference each other, thereby loosely coupling them, and their interactions can be changed independently.
Introduction
Intent
By introducing a mediator object to encapsulate and coordinate interactions between multiple objects, the coupling between objects is reduced.
Main problem solved
- Solves the complex one-to-many association problem between objects, avoids high coupling between objects, and simplifies the system structure.
Use Cases
- When multiple classes in the system are coupled to each other, forming a network structure.
Implementation
- Define the mediator interface: Specifies the interface the mediator must implement.
- Create the concrete mediator: Implements the mediator interface and contains the logic to coordinate the interactions of colleague objects.
- Define the colleague classes: The objects do not need to explicitly reference each other, but interact 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 traded through the WTO, simplifying bilateral relations.
- Airport scheduling 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 one-to-many relationships among multiple objects into one-to-one relationships.
- Decoupling: Objects no longer reference each other directly and interact through the mediator.
- Complies with the Law of Demeter: An object only needs to know the mediator, not other objects.
Disadvantages
- Mediator complexity: The mediator may become large and complex, difficult to maintain.
Usage recommendation
- When complex reference relationships exist between objects in the system, consider using the Mediator Pattern.
- Encapsulate the behavior of multiple classes through the mediator, avoiding the generation of too many subclasses.
Notes
- Avoid using the Mediator Pattern when responsibilities are unclear or chaotic, which may cause the mediator to take on too many responsibilities.
structures
The Mediator Pattern contains the following main roles:
-
Mediator (Mediator): Defines an interface for communicating with colleague objects and managing the relationships between them. Usually includes one or more event handling methods for handling various interaction events.
-
Concrete Mediator (Concrete Mediator): Implements the mediator interface and is responsible for implementing the communication logic between colleague objects. It maintains references to the colleague objects and coordinates their interactions.
-
Colleague Object (Colleague): Defines an interface for communicating with the mediator. Usually includes a method for sending messages and a method for receiving messages.
-
Concrete Colleague Object (Concrete Colleague): Implements the colleague object interface and is the object that actually participates in the interaction. It sends its own messages to the mediator, which forwards them to other colleague objects.
Create a class that implements
We demonstrate the Mediator Pattern through a chat room example. In the example, multiple users can send messages to the chat room, and the chat room displays messages to all users. We will create two classes ChatRoom and User。User objects use the ChatRoom method to share their messages.
MediatorPatternDemo, and our demo class uses the User object to display the communication between them.
Step 1
Create the mediator class.
ChatRoom.java
import
java
.
util
.
Date
;
public
class
ChatRoom
{
public
static
void
showMessage
(
User
user
,
String
message
)
{
System
.
out
.
println
(
new
Date
(
)
.
toString
(
)
"
[
"
user
.
getName
(
)
"
] :
"
message
)
;
}
}
Step 2
Create the user class.
User.java
public
class
User
{
private
String
name
;
public
String
getName
(
)
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
User
(
String
name
)
{
this
.
name
=
name
;
}
public
void
sendMessage
(
String
message
)
{
ChatRoom
.
showMessage
(
this
,
message
)
;
}
}
Step 3
Use User object to display the communication between them.
MediatorPatternDemo.java
public
class
MediatorPatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
User
robert
=
new
User
(
"
Robert
"
)
;
User
john
=
new
User
(
"
John
"
)
;
robert
.
sendMessage
(
"
Hi! John!
"
)
;
john
.
sendMessage
(
"
Hello! Robert!
"
)
;
}
}
Step 4
Run the program, output result:
Thu Jan 31 16:05:46 IST 2013 [Robert] : Hi! John!
Thu Jan 31 16:05:46 IST 2013 [John] : Hello! Robert!