Defines how an object encapsulates a series of multiple objects to interact with each other. Mediator mediator allows objects to be referenced from each other without being displayed, making coupling loose. It also allows us to independently change the interaction of multiple objects.
The UML class diagram is as follows:
The relationship between classes and objects is:
1. mediator (Abstract intermediary): defines an interface for communication with colleags.
2. concretemediator (a specific intermediary): coordinates the actions of colleagues to achieve collaboration, master and maintain the reference of each colleague object.
3. colleague (colleague class): Each colleague object references an intermediary object. Each colleague object communicates with its intermediary when it needs to communicate with other colleagues.
The following figure shows the order of typical applications:
Communication between colleagues is transmitted through media.
Many participants are registered in the chat room, which serves as an intermediary for communication between participants. Through it, participants can send messages to any other participants without restriction (loose coupling ). Its UML class diagram is as follows:
View code
// Intermediary Interface
Interface Ichatroom
{
Void Register (maid );
Void Send ( String From, String To, String Message );
}
// Intermediary-chat room
Class Chatroom: ichatroom
{
// Participants
Private Hashtable participant ipants = New Hashtable ();
// Register participants first
Public Void Register (maid)
{
If (Maid [maid. Name] = Null )
Maid [maid. Name] = maid;
Participant. chatroom = This ;
}
// Send information
Public Void Send ( String From, String To, String Message)
{
Particle ant PTO = (particle ant) maid [to];
If (PTO! = Null )
PTO. Receive (from, message );
}
}
// Participant base class
Class Participant
{
Private Chatroom;
Private String Name;
Public Particle ( String Name)
{
This . Name = Name;
}
// Has chat room attributes
Public Chatroom
{
Get { Return Chatroom ;}
Set {Chatroom = value ;}
}
Public String Name
{
Get { Return Name ;}
}
// Send a message and call the sending method of the chat room class
Public Void Send ( String To, String Message)
{
Chatroom. Send (name, to, message );
}
// Virtual receiving function
Public Virtual Void Receive ( String From, String Message)
{
Console. writeline ( " {0} Yo {1}: '{2 }' " , From, This . Name, message );
}
}
// Colleagues-Beatle participants
Class Beatleparticipant ipant: maid
{
Public Beatleparticipant ipant ( String Name ): Base (Name ){}
Public Override Void Receive ( String From,String Message)
{
Console. Write ( " To a Beatle " );
Base . Receive (from, message );
}
}
// Specific colleague class-non-Beatle participant
Class Nonbeatleparticipant ipant: maid
{
Public Nonbeatleparticipant ipant ( String Name ): Base (Name ){}
Public Override Void Receive ( String From, String Message)
{
Console. Write ( " To a non-Beatle " );
Base . Receive (from, message );
}
}
// Intermediary mode testProgram
Class Program
{
Static Void Main ( String [] ARGs)
{
// Create chat room
Chatroom c = New Chatroom ();
// Create participants and log on to the chat room
Particle ant George = New Beatleparticipant ipant ( " George " );
Particle ant Paul = New Beatleparticipant ipant ( " Paul " );
Participant Ringo = New Beatleparticipant ipant (" Ringo " );
Particle ant John = New Beatleparticipant ipant ( " John " );
Particle ant Yoko = New Nonbeatleparticipant ipant ( " Yoko " );
C. Register (George );
C. Register (Paul );
C. Register (Ringo );
C. Register (John );
C. Register (Yoko );
// Chat between Members
Yoko. Send ( " John " , " Hi John! " );
Paul. Send ( " Ringo " ," All you need is love " );
Ringo. Send ( " John " , " My sweet lord " );
Paul. Send ( " John " , " Can't buy me love " );
John. Send ( " Yoko " , " My sweet love " );
Console. Read ();
}
}
- Advantages and disadvantages:
The intermediary mode separates two colleagues' classes, simplifies object protocols, and controls object interaction in the center, making individual objects easier and easier, because it does not need to pass data to other individual objects, it only needs to be passed to the intermediary. An individual object does not need to have the logic to process internal communication, so it highlights its object-oriented features.
The following scenarios are suitable for the intermediary mode:
1. A group of objects communicate with each other in a complex manner, but their methods are clearly defined.
2. Several objects need to be defined without subclass implementation.
the intermediary mode is usually used in the information communication-based system structure. This information is transmitted to the associated object without direct link.