Mediator pattern - coordinates interactions between multiple objects

1 Introduction

1.1. Overview

If the relationship between objects in a system presents a network structure, as shown in the following figure:
insert image description here
there are a large number of many-to-many relationships between objects, which will lead to a very complex system. These objects will not only affect other objects, but also be affected Influenced by other objects, these objects are called colleagues objects, and they realize the behavior of the system through the interaction with each other. In a network structure, almost every object needs to interact with other objects, and this interaction appears as a direct coupling of one object to another object, which will lead to an over-coupled system.

The Mediator pattern can drastically reduce the number of relationships between objects. By introducing a mediator object, the network structure of the system can be changed into a star structure centered on the mediator, as shown in the following figure:
insert image description here
In this star structure, the colleague object is no longer directly connected with another object, it passes A mediator object interacts with another object. The existence of the intermediary object ensures the stability of the object structure, that is to say, the structure of the system will not bring a lot of modification work due to the introduction of new objects.

If there is a many-to-many relationship between objects in a system, some interactive behaviors between objects can be separated from each object, and encapsulated in an intermediary object, and the intermediary will perform unified coordination. In this way, the many-to-many complex relationship between objects is transformed into a relatively simple one-to-many relationship. By introducing intermediaries to simplify complex interactions between objects, the intermediary pattern is a typical application of Demeter's law.

1.2. Definition

Mediator Pattern: Use a mediator object (mediator) to encapsulate a series of object interactions. Mediators remove the need for objects to explicitly reference each other, making them loosely coupled, and their interactions can be changed independently. Mediator mode, also known as mediator mode, is an object-behavioral mode.

2. Analysis

2.1, UML class diagram

In the intermediary mode, the intermediary class used to coordinate mutual calls between other objects/classes is introduced. In order to make the system more flexible and scalable, an abstract intermediary is usually provided, and its structure diagram is shown in the figure below.
insert image description here
The following four roles are included in the intermediary model structure diagram:

  1. Mediator (abstract mediator): It defines an interface, which is used to communicate with each colleague object.
  2. ConcreteMediator (concrete mediator): It is a subclass of the abstract mediator, which realizes collaborative behavior by coordinating each colleague object and maintains a reference to each colleague object.
  3. Colleague (abstract colleague class): It defines the public methods of each colleague class, and declares some abstract methods for subclasses to implement, and maintains a reference to the abstract intermediary class through which subclasses can communicate with the intermediary or communication.
  4. ConcreteColleague (concrete colleague class): It is a subclass of abstract colleague class. When each colleague object needs to communicate with other colleague objects, it first communicates with the intermediary, and indirectly completes the communication with other colleague classes through the intermediary. An abstract method declared in an abstract colleague class is implemented in a concrete colleague class.

The core of the mediator pattern is the introduction of the mediator class. In the intermediary model, the intermediary class assumes the following two responsibilities:
(1) Transition role (structural). Through the relay function provided by the mediator, each colleague object no longer needs to explicitly refer to other colleagues. When it is necessary to communicate with other colleagues, an indirect call can be implemented through a mediator. This transfer role belongs to the structural support of the intermediary.
(2) Coordination (behavioral). The intermediary can further encapsulate the relationship between colleagues, and colleagues can interact with the intermediary in a consistent manner without specifying what the intermediary needs to do. The intermediary further processes colleagues' requests according to the coordination logic encapsulated in itself, and separates and encapsulates the relationship behaviors between colleagues. This coordinating role belongs to the mediator's support in behavior.

2.2. Code example

In the intermediary mode, a typical abstract intermediary class code is as follows:

/**
 * @Description: 抽象中介者
 * @Author: yangyongbing
 * @CreateTime: 2023/08/03  08:56
 * @Version: 1.0
 */
public class Mediator {
    
    

    // 存储同事对象
    protected ArrayList<Colleague> colleagues;

    // 注册方法,用于增加同事对象
    public void register(Colleague colleague){
    
    
        colleagues.add(colleague);
    }

    // 声明抽象的业务方法
    public void operation() {
    
    

    }
}

A collection of colleague classes can be defined in the abstract intermediary, which is used to store colleague objects and provide registration methods, and at the same time declare the methods of the concrete intermediary class. These abstract methods will be implemented in the concrete intermediary class, and the typical concrete intermediary class code is as follows:

/**
 * @Description: 具体中介者
 * @Author: yangyongbing
 * @CreateTime: 2023/08/03  08:58
 * @Version: 1.0
 */
public class ConcreteMediator extends Mediator{
    
    
    // 实现业务方法,封装同事之间的调用
    public void operation(){
    
    
        // 通过中介者调用同事类的方法
        colleagues.get(0).method();
    }
}

In the specific intermediary class, the method of the colleague class will be called, and some business codes of one's own can be added to control the call when calling.

A reference to the abstract mediator is maintained in the abstract colleague class, which is used to call the method of the mediator. A typical abstract colleague class code is as follows:

/**
 * @Description: 抽象同事类
 * @Author: yangyongbing
 * @CreateTime: 2023/08/03  09:02
 * @Version: 1.0
 */
public class Colleague {
    
    
    // 维持一个抽象中介者的引用
    protected Mediator mediator;

    public Colleague(Mediator mediator) {
    
    
        this.mediator = mediator;
    }

    // 声明自身方法,处理自己的行为
    public void method() {
    
    

    }

    // 定义依赖方法,与中介者进行通信
    public void dependencyMethod(){
    
    
       mediator.operation();
    }
}

The abstract methods of the colleague class are declared in the abstract colleague class, and these methods will be implemented in the concrete colleague class. A typical specific colleague class code is as follows:

/**
 * @Description: 具体同事类
 * @Author: yangyongbing
 * @CreateTime: 2023/08/03  09:06
 * @Version: 1.0
 */
public class ConcreteColleague extends Colleague{
    
    
    public ConcreteColleague(Mediator mediator) {
    
    
        super(mediator);
    }

    // 实现自身方法
    public void method(){
    
    

    }
}

The method declared in the abstract colleague class is implemented in the concrete colleague class ConcreteColleague. The method method () is the self-method (Self-Method) of the colleague class, which is used to deal with its own behavior. The method dependencyMethod() is a dependent method (Depend-Method), which is used to call the method defined in the intermediary and rely on the intermediary to complete the corresponding behavior, such as calling a related method of another colleague class.

3. Summary of intermediary model

The Mediator pattern turns a mesh system structure into a star structure centered on the Mediator object. In this star structure, the many-to-many relationship between the original objects is replaced by the one-to-many relationship between the mediator object and other objects. The intermediary model is widely used in event-driven software, especially GUI-based application software. In addition, the mediator pattern has been well applied in systems with intricate associations between classes.

3.1. Main advantages

  1. The mediator pattern simplifies the interaction between objects, it replaces the original many-to-many interaction between colleagues with the one-to-many interaction between the mediator and colleagues. One-to-many relationships are easier to understand, maintain, and extend, transforming an otherwise incomprehensible mesh structure into a relatively simple star structure.
  2. The mediator pattern decouples each colleague object. The intermediary is conducive to the loose coupling between colleagues, and each colleague and intermediary can be changed and reused independently. It is more convenient to add new intermediaries and new colleagues, and it better conforms to the principle of opening and closing.
  3. Can reduce the generation of a large number of colleague subclasses. The intermediary gathers the behaviors that were originally distributed between multiple objects, and changing these behaviors only needs to generate a new intermediary subclass, which makes each colleague class can be reused without extending the colleague class.

3.2. Main disadvantages

The main disadvantage of the intermediary model is that a large number of interaction details between colleagues are included in the specific intermediary class, which may cause the specific intermediary class to be very complicated and make the system difficult to maintain.

3.3. Applicable scenarios

  1. There are complex reference relationships between objects in the system, and the system structure is confusing and difficult to understand.
  2. An object is difficult to reuse because it refers to many other objects and communicates directly with these objects.
  3. I want to encapsulate the behavior of multiple classes through an intermediate class, but I don't want to generate too many subclasses. It can be realized by introducing a mediator class, defining the public behavior of object interaction in the mediator, and adding a new specific mediator class if the behavior needs to be changed.

Guess you like

Origin blog.csdn.net/YYBDESHIJIE/article/details/132073798