Mediator Pattern

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.

Description

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.

Advantages

  • Simplifies the interaction between objects.
  • Decouples colleagues.
  • Reduces the generation of subclasses.
  • Simplifies the design and implementation of colleague classes.

Disadvantages

  • 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.

Applicable Environment

  • 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.

Implementation

// Use a chat room as a mediator to provide users with chat functionality
class ChatRoom {
    showMessage(user, message) {
        const time = new Date();
        const sender = user.getName();
        console.log(`${time} [${sender}]: ${message}`)
    }
}

class User {
    constructor(name, chatMediator) {
        this._name = name;
        this.chatMediator = chatMediator;
    }
    
    getName() {
        return this._name;
    }
    
    send(message) {
        this.chatMediator.showMessage(this, message);
    }
}

(function(){
    const mediator = new ChatRoom();
    const john = new User("John Doe", mediator);
    const jane = new User("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!
})();

Daily Question

https://github.com/WindrunnerMax/EveryDay

References

https://www.runoob.com/design-pattern/mediator-pattern.html https://github.com/sohamkamani/javascript-design-patterns-for-humans#-mediator https://design-patterns.readthedocs.io/zh_CN/latest/behavioral_patterns/mediator.html