Design pattern -- mediator pattern (mediator pattern)

Posted by suttercain on Wed, 06 Oct 2021 18:37:20 +0200

A module may be composed of multiple objects, and there may be mutual references between these objects, as shown in the following figure:

  In order to reduce the complex reference relationship between objects and reduce the coupling degree of the system, the mediator pattern is introduced, which is also the motivation for the creation of the mediator pattern.

Definition given by GoF:

The mediator pattern is to encapsulate a series of object interactions with a mediator object. The mediator makes the objects do not need to show mutual references, so that their coupling is loose, so that their interaction can be changed independently.

Mediator pattern is an object behavior pattern that encapsulates the relationship between objects. In this design, each object can communicate with it through mediator objects without knowing the specific information of other objects.

Advantages: reduce the relationship between system objects and improve the reusability and scalability of objects

Disadvantages: mediator objects can become large and complex

  The UML class diagram of the mediator is as follows:

1. Mediator (Abstract mediator)

Define an interface to communicate with colleagues

2. Concretemediator (specific mediator)

Coordinate the cooperative behavior of each colleague object, understand and maintain the relationship between each colleague object

3. Collague (Abstract colleague class)

Define the colleague class interface and define the common methods of each colleague

4. Concrete colleague

Implement methods in abstract classes. Each colleague class needs to know the mediator object. Each specific colleague class only needs to understand its own behavior, not how to implement other colleague classes. When it needs to communicate with other colleague classes, it needs to communicate before the mediator object to realize indirect calls.

  The core of the intermediary model is the introduction of the intermediary class. In this model, the intermediary model undertakes two responsibilities:

1. Transit effect

Similar to the role of the base station when making a phone call, if all colleagues want to communicate with other objects, they must communicate with the mediator object, which manages the communication between them.

2. Coordinating role

  Coordinate the relationship between objects

One of the most widely used scenarios of the mediator pattern is message oriented middleware

To test:

Test examples include landlords, intermediaries and graduating college students

analysis:

1. Both the landlord and the graduating college students belong to Person, so they are abstracted to establish an abstract class

2. There may be multiple intermediaries, so establish an abstract intermediary

3. Establish two entity classes: landlord class and graduating college students

4. Create a specific mediation class

public abstract class Person {
    Mediator m_mediator;
    abstract void SetMediator(Mediator mediator);
    abstract void SendMessage(String message);
    abstract void getMessage(String message);
}
public interface Mediator {
    void Send(String message, Person person);
    void SetA(Person person);
    void SetB(Person person);
}
public class Rentor extends Person {
    @Override
    public void SetMediator(Mediator mediator) {
        super.m_mediator = mediator;
    }

    @Override
    public void SendMessage(String message) {
        super.m_mediator.Send(message,this);
    }

    @Override
    public void getMessage(String message) {
        System.out.println("Tenant received message: " + message);
    }
}
public class Landlord extends Person {
    @Override
    void SetMediator(Mediator mediator) {
        super.m_mediator = mediator;
    }

    @Override
    void SendMessage(String message) {
        super.m_mediator.Send(message,this);
    }

    @Override
    void getMessage(String message) {
        System.out.println("The landlord received a message: " + message);
    }
}
public class HouseMediator implements Mediator{
    private Person personA,personB;
    @Override
    public void Send(String message, Person person) {
        if (person.equals(personA)){
            personB.getMessage(message);
        }else if(person.equals(personB)){
            personA.getMessage(message);
        }
    }

    @Override
    public void SetA(Person person) {
        personA = person;
    }

    @Override
    public void SetB(Person Person) {
        personB = Person;
    }
}

Test code

public class Test {

    public static void main(String[] args) {
        //Establish an intermediary
        Mediator mediator = new HouseMediator();
        //Create landlords and tenants
        Person rendor = new Rentor();
        Person Landlord = new Landlord();
        //The mediator establishes a connection with the tenant
        mediator.SetA(rendor);
        //The intermediary establishes a connection with the landlord
        mediator.SetB(Landlord);
        //Contact landlord and tenant
        //news
        String message = "There is a vacant house";
        //The landlord sends a message to the intermediary
        mediator.Send(message,Landlord);
        //Messages sent by tenants to intermediaries
        String messageRendor = "OK, I'll see the house tomorrow";
        //The intermediary sends the message to the landlord
        mediator.Send(messageRendor, rendor);
    }
}

  result:

The tenant received a message: there is a vacant house
 The landlord received the news: OK, go to see the house tomorrow

Topics: Java Design Pattern