This is the first day of my participation in the Gwen Challenge in November. Check out the details: the last Gwen Challenge in 2021
Mediator Pattern is used to reduce the complexity of communication between multiple objects and classes. This pattern provides a mediation class that typically handles communication between different classes and supports loose coupling, making code easy to maintain.
The mediator pattern is a behavioral pattern.
introduce
describe | parsing |
---|---|
intentions | By encapsulating a set of object interactions with a mediation object, the mediator loosens the coupling of objects without explicitly referring to each other, and can change their interactions independently. |
Mainly to solve | There are a large number of related relationships between objects, which will inevitably lead to the complex structure of the system. At the same time, if an object changes, we also need to track the objects associated with it and make corresponding processing at the same time. |
When to use | Multiple classes are coupled to form a network structure. |
How to solve | The above network structure is separated into star structure. |
The key code | Communication between objects is encapsulated in a class and handled separately. |
Examples of application | 1. Before China joined the WTO, countries traded with each other with a complex structure. Now countries trade with each other through the WTO. 2. Airport scheduling system. 3. MVC framework, in which C (controller) is the mediator between M (model) and V (view). |
advantages | 1. Reduce the class complexity and transform one-to-many into one-to-one. 2. Decoupling between classes. 3, in accordance with the Demeter principle. |
disadvantages | Intermediaries can be large, complex and difficult to maintain. |
Usage scenarios | 1. Complex reference relationships exist among objects in the system, resulting in disorganized structure of the dependency relationship between them and difficulty in reusing the object. 2. You want an intermediate class to encapsulate behavior in multiple classes without generating too many subclasses. |
Matters needing attention | Should not be used when duties are confused. |
implementation
Illustrate the mediator pattern with an example chat room. In this example, multiple users can send messages to a 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, the scenario class uses the User object to show the communication between them.
Step 1- Create the mediation class
Create the mediation class, chatroom.java
// 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); }}Copy the code
Step 2- Create the user class
Create the User class user.java
// 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); }}Copy the code
Step 3- Scenario class
Create the scene class MediatorPatternDemo.java
// 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!" ); }}Copy the code
Step 4- Print out
Execute the program and 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!
Copy the code