Java implements the Mediator pattern

The Mediator pattern is a behavioral design pattern, which is realized by transferring the interaction between objects to the mediator object. In this pattern, objects do not directly communicate with each other, but communicate and coordinate through intermediary objects. Applicable scenario: 1. When the change of one object needs to change other objects at the same time, the Mediator pattern can be used. By introducing intermediary objects, the coupling between objects can be reduced, reducing direct interaction between objects. 2. When an interaction process involves a large number of interactions between objects, resulting in complex interactions, the Mediator pattern can be used to simplify the interaction process. Benefits: 1. Reduced direct coupling between objects, making the code easier to maintain and expand. 2. Focus on the intermediary object to make the code clearer and easier to understand. 3. It can avoid circular dependencies between objects and improve code flexibility. The following is an example code of using Java to implement the Mediator pattern: //Mediator Interface interface Mediator { void sendMessage(String message, Colleague colleague); } //Specific intermediaries class ConcreteMediator implements Mediator { private ColleagueA colleagueA; private ColleagueB colleagueB; public void setColleagueA(ColleagueA colleagueA) { this.colleagueA = colleagueA; } public void setColleagueB(ColleagueB colleagueB) { this.colleagueB = colleagueB; } @Override public void sendMessage(String message, Colleague colleague) { if (colleague == colleagueA) { colleagueB.receiveMessage(message); } else if (colleague == colleagueB) { colleagueA.receiveMessage(message); } } } //Abstract colleague class abstract class Colleague { protected Mediator mediator; public Colleague(Mediator mediator) { this.mediator = mediator; } public abstract void sendMessage(String message); public abstract void receiveMessage(String message); } //Specific colleague category A class ColleagueA extends Colleague { public ColleagueA(Mediator mediator) { super(mediator); } @Override public void sendMessage(String message) { mediator.sendMessage(message, this); } @Override public void receiveMessage(String message) { System.out.println("ColleagueA received message: " + message); } } //Specific colleague category B class ColleagueB extends Colleague { public ColleagueB(Mediator mediator) { super(mediator); } @Override public void sendMessage(String message) { mediator.sendMessage(message, this); } @Override public void receiveMessage(String message) { System.out.println("ColleagueB received message: " + message); } } //Testing class public class MediatorPatternDemo { public static void main(String[] args) { ConcreteMediator mediator = new ConcreteMediator(); ColleagueA colleagueA = new ColleagueA(mediator); ColleagueB colleagueB = new ColleagueB(mediator); mediator.setColleagueA(colleagueA); mediator.setColleagueB(colleagueB); colleagueA.sendMessage("Hello, colleagueB!"); colleagueB.sendMessage("Hi, colleagueA!"); } } In the above example code, the Mediator pattern realizes communication and coordination between objects through the implementation of the mediator interface and the specific mediator class. Abstract colleague classes define the common behavior of colleague classes and their dependence on intermediaries. Specific colleague classes inherit abstract colleague classes and implement their respective methods. The test class creates the mediator and colleague class instance, sets the corresponding colleague class instance through the mediator, and then calls the method of the colleague class to send the message. Through the intermediary's forwarding, the colleague class receives the message sent by the other party and processes it.