Design Patterns - Mediator Pattern

Mediator Pattern - Mediator Pattern

Mediator Pattern : A mediator object (mediator) is used to encapsulate a series of object interactions. The mediator makes the objects do not need to refer to each other explicitly, so that the coupling is loose, and they can be changed independently. interaction between. The mediator mode, also known as the mediator mode, is an object behavioral mode.

My understanding: take the mediator as the center, send a message to other or specified colleague classes, define a method in the abstract mediator, indicating that the specific mediator needs to process the received message (sender and message content), each Each colleague class needs to declare who its mediator is when it is instantiated. The mediator also needs to know which class of colleagues it is.

The mediator pattern turns a networked system structure into a star-shaped structure centered on the mediator object . In this star-shaped structure, the one-to-many relationship between the mediator object and other objects is used to replace the original objects. many-to-many relationship.

The example comes from here  https://www.cnblogs.com/ysw-go/p/5413958.html

      ● Abstract Mediator: It defines an interface, the interface is used to communicate with various colleague objects. 

      ● Concrete mediator: It is a subclass of abstract mediator, which realizes cooperative behavior by coordinating each colleague object, and it maintains a reference to each colleague object. 

      ● Abstract colleague class: It defines the public methods of each colleague class, and declares some abstract methods for subclasses to implement. At the same time, it maintains a reference to the abstract mediator class, and its subclasses can communicate with the mediator through this reference. communication. 

      ● Concrete colleague class: it is a subclass of abstract colleague class; when each colleague object needs to communicate with other colleague objects, it communicates with the intermediary first, and indirectly completes the communication with other colleague classes through the intermediary; in the concrete colleague class The abstract methods declared in the abstract colleague class are implemented in . 

abstract mediator

// abstract mediator class
abstract class Mediator{
    abstract void contact(String content,Colleague coll);
}

abstract colleague class

abstract class Colleague {
    protected String name;
    protected Mediator mediator;

    public Colleague(String name, Mediator mediator) {
        this.name = name;
        this.mediator = mediator;
    }
}
specific class of colleagues

class ColleagueA extends Colleague {

    // The specific colleague class inherits from Colleague and can communicate with the mediator at this moment
    public ColleagueA(String name, Mediator mediator) {
        super(name, mediator);
    }
    public void getMessage(String message){
        System.out.println("Colleague A"+name+"Get information"+message);
    }
    // Colleague A communicates with the mediator
    public void contact(String message){
        System.out.println("Colleague A sent a message");
        mediator.contact(message, this);
    }
}
class ColleagueB extends Colleague {

    public ColleagueB(String name, Mediator mediator) {
        super(name, mediator);
    }
    public void getMessage(String message){
        System.out.println("Colleague B"+name+"Get information"+message);
    }
    // Colleague B communicates with the mediator
    public void contact(String message){
        System.out.println("Colleague B sent a message");
        mediator.contact(message, this);
    }
}

specific intermediary

// specific mediator
class ConcreteMediator extends Mediator {
    ColleagueA collA;
    ColleagueB collB;

    public void setCollA(ColleagueA collA) {
        this.collA = collA;
    }

    public void setCollB(ColleagueB collB) {
        this.collB = collB;
    }

    @Override
    public void contact(String content, Colleague coll) {
        if (coll==collA) {
            collB.getMessage(content);
        } else {
            collA.getMessage(content);
        }
    }
}

Client

public class Client {

    /**
     * @param args
     */
    // Mediator, ColleagueA, ColleagueB
    public static void main(String[] args) {
        // define the mediator
        ConcreteMediator mediator = new ConcreteMediator();
        //Define the specific colleague class
        ColleagueA colleagueA = new ColleagueA("张三", mediator);
        ColleagueB colleagueB = new ColleagueB("李四", mediator);
        // Mediator knows about each concrete Colleague class
        mediator.setCollA(colleagueA);
        mediator.setCollB(colleagueB);
        colleagueA.contact("I am A, I want to talk to colleague B about work");
        colleagueB.contact("I'm B, I have time in the afternoon, let's discuss it in the afternoon");
    }

}



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326942822&siteId=291194637