YouTip LogoYouTip

Mediator Pattern

# Mediator Pattern The Mediator Pattern is a behavioral design pattern used to reduce the communication complexity between multiple objects and classes. The Mediator Pattern defines a mediator object that encapsulates a series of interactions between objects. The mediator allows objects to not explicitly reference each other, thereby loosening their coupling and allowing their interactions to be changed independently. ## Introduction ### Intent To reduce the coupling between objects by introducing a mediator object to encapsulate and coordinate the interactions among multiple objects. ### Main Problem Solved * Solves the problem of complex one-to-many associations between objects, avoiding high coupling between objects and simplifying the system structure. ### Use Cases * When multiple classes in a system are coupled to each other, forming a network structure. ### Implementation Approach * **Define the Mediator Interface**: Specifies the interface that the mediator must implement. * **Create Concrete Mediator**: Implements the mediator interface and contains the logic for coordinating the interactions among colleague objects. * **Define Colleague Classes**: Each object does not need to explicitly reference others but interacts through the mediator. ### Key Code * **Mediator**: Encapsulates the interaction logic between objects. * **Colleague Class**: Communicates through the mediator. ### Application Examples 1. **WTO**: After China joined the WTO, countries conduct trade through the WTO, simplifying bilateral relationships. 2. **Airport Scheduling System**: Coordinates aircraft takeoffs, landings, runway usage, etc. 3. **MVC Framework**: The controller acts as a mediator between the model and the view. ### Advantages 1. **Reduces Complexity**: Converts one-to-many relationships between multiple objects into one-to-one relationships. 2. **Decoupling**: Objects no longer reference each other directly; they interact through the mediator. 3. **Follows the Law of Demeter**: Objects only need to know the mediator, not other objects. ### Disadvantages * **Mediator Complexity**: The mediator may become large and complex, making it difficult to maintain. ### Usage Recommendations * Consider using the Mediator Pattern when there are complex reference relationships between objects in a system. * Encapsulate the behavior of multiple classes through the mediator to avoid creating too many subclasses. ### Notes * Avoid using the Mediator Pattern when responsibilities are unclear or chaotic, as this may lead to the mediator taking on 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 them. It typically 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**: Defines an interface for communicating with the mediator. It typically includes a method for sending messages and a method for receiving messages. * **Concrete Colleague**: Implements the colleague interface and is the object that actually participates in the interactions. It sends its messages to the mediator, which then forwards them to other colleague objects. ## Implementation We will demonstrate the Mediator Pattern using a chat room example. In this example, multiple users can send messages to the chat room, and the chat room displays the messages to all users. We will create two classes, _ChatRoom_ and _User_. The _User_ objects use the _ChatRoom_ method to share their messages. _MediatorPatternDemo_, our demo class, uses _User_ objects to display their communication. ![Image 2: UML diagram of the Mediator Pattern](#) ### 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_ objects to display their communication. ## 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 Execute the program, output: Thu Jan 31 16:05:46 IST 2013 : Hi! John!Thu Jan 31 16:05:46 IST 2013 : Hello! Robert!
← Memento PatternIterator Pattern β†’