The Mediator Pattern encapsulates a series of object interactions with a mediator object, which allows objects to interact without explicitly referencing each other, thereby loosening their coupling. This reduces the complexity of communication between multiple objects and classes, and allows the interaction between them to be independently changed. The mediator class usually handles communication between different classes. The Mediator Pattern is also known as the Colleague pattern and is a type of behavioral pattern.
In software engineering, the Mediator pattern defines an object that encapsulates the way a group of objects interact. Since this pattern can change the runtime behavior of a program, it is considered a behavioral pattern. In a design scenario where users chat directly with each other, there is a strong correlation between user objects, leading to the following problems: complex system structure, a large amount of mutual relationships and calls between objects, the need to track and handle all other objects associated with a changed object, and poor reusability of objects due to strong associations with other objects. This makes them seem more like indivisible wholes with confused responsibilities. The system's scalability is also low, as adding a new object requires adding references to existing related objects and adjusting the original objects, resulting in a high coupling between objects, inflexible operations, and poor scalability. In the process of object-oriented software design and development, according to the single responsibility principle, we should refine objects as much as possible so that they are only responsible for or present a single responsibility. A module may consist of many objects, and these objects may have mutual references. To reduce the complex reference relationships between objects and make them a loosely coupled system, the Mediator pattern is needed.
The specific mediator class includes the interaction details between colleagues, which may make the specific mediator class very complex and make the system difficult to maintain.
The objects in the system have complex reference relationships, leading to a confusing and difficult-to-understand mutual dependency structure.
An object is difficult to reuse because it has referenced many other objects and communicates directly with them.
When you want to encapsulate the behavior of multiple classes through an intermediary class without generating too many subclasses, you can introduce a mediator class to define objects in the mediator.
Common behaviors of interaction, if the behavior needs to be changed, new mediator classes can be added.
// Use a chat room as a mediator to provide users with chat functionalityclassChatRoom{showMessage(user, message){const time =newDate();const sender = user.getName(); console.log(`${time} [${sender}]: ${message}`)}}classUser{constructor(name, chatMediator){this._name = name;this.chatMediator = chatMediator;}getName(){returnthis._name;}send(message){this.chatMediator.showMessage(this, message);}}(function(){const mediator =newChatRoom();const john =newUser("John Doe", mediator);const jane =newUser("Jane Doe", mediator); john.send("Hi there!");// Tue Nov 10 2020 19:36:07 GMT+0800 (China Standard Time) [John Doe]: Hi there! jane.send("Hey!");// Tue Nov 10 2020 19:36:07 GMT+0800 (China Standard Time) [Jane Doe]: Hey!})();