The intermediary pattern (Mediator Pattern) is used to reduce the complexity of communication between multiple objects and classes. This pattern provides a mediation class that usually handles communication between different classes and supports loose coupling, making the code easy to maintain. The intermediary model belongs to the behavioral model. 意图: A mediation object is used to encapsulate a series of object interactions, and the mediator makes the objects do not need to explicitly reference each other, so that they are loosely coupled, and the interaction between them can be changed independently. 主要解决: There are a large number of relationships between objects, which is bound to cause the structure of the system to become very complex. at the same time, if an object changes, we also need to track the objects associated with it and deal with them accordingly. 何时使用: Multiple classes are coupled to each other to form a network structure. 如何解决: The above reticular structure is separated into a star structure. 关键代码: Communication between object Colleague is encapsulated into a class to handle separately. 应用实例: 1. Before China joined the WTO, various countries traded with each other, and the structure was complex. Now, various countries trade with each other through WTO. 2. Airport dispatching system. 3. MVC framework, in which C (controller) is the intermediary between M (model) and V (view). 优点: 1. Reduce the complexity of the class and transform one-to-many into one-to-one. 2. Decoupling between different classes. 3. It conforms to Dimitt’s principle. 缺点: Intermediaries can be large and complex to maintain. 使用场景: 1. There is a complex reference relationship between objects in the system, which leads to confusion in the structure of the dependency relationship between them and it is difficult to reuse the object. 2. Want to encapsulate the behavior of multiple classes through an intermediate class, but do not want to generate too many subclasses. 注意事项: It should not be used when duties are confused. We demonstrate the intermediary model through a chat room example. In an example, multiple users can send messages to the chat room, and the chat room displays the message to all users. We will create two classes ChatRoom And User . User Object usage ChatRoom To share their information. MediatorPatternDemo Our demo class uses the User Object to display the communication between them. Create a mediation class. Create the user class. Use User Object to display the communication between them. Execute the program and output the result: 6.20.1. Introduction ¶
6.20.2. Realize ¶

6.20.3. Step 1 ¶
ChatRoom.java ¶
importjava.util.Date;publicclassChatRoom{publicstaticvoidshowMessage(Useruser,Stringmessage){System.out.println(newDate().toString()+"["+user.getName()+"]
:"+message);}}
6.20.4. Step 2 ¶
User.java ¶
publicclassUser{privateStringname;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicUser(Stringname){this.name=name;}publicvoidsendMessage(Stringmessage){ChatRoom.showMessage(this,message);}}
6.20.5. Step 3 ¶
MediatorPatternDemo.java ¶
publicclassMediatorPatternDemo{publicstaticvoidmain(String[]args){Userrobert=newUser("Robert");Userjohn=newUser("John");robert.sendMessage("Hi!
John!");john.sendMessage("Hello! Robert!");}}
6.20.6. Step 4 ¶
Thu Jan 31 16:05:46 IST 2013 [Robert] : Hi! John!
Thu Jan 31 16:05:46 IST 2013 [John] : Hello! Robert!