introduce

  1. The Mediator Pattern encapsulates a series of object interactions with a Mediator object. The Mediator makes each object not show reference to each other, thus loosens its coupling, and can independently change the interaction between them.
  2. Mediators are behavioral patterns that make code easy to maintain.
  3. For example, MVC pattern C (Controller Controller) is the mediator between M (Model Model) and V (View view)

  • Mediator: An abstract Mediator that defines the interface between colleague objects and Mediator objects
  • Colleague: is an abstract Colleague class
  • ConcreteMediator: a ConcreteMediator object that implements an abstract class method that needs to know all concrete colleague classes, receive messages from concrete colleagues, and issue commands to concrete colleague objects
  • ConcreteColleague: colleagues are concrete classes, each concrete colleagues only know their own behavior, and don’t know other colleagues classes, but they all know mediator object.

case

  1. There are many agencies in our life now, such as Lianjia, I love my family and so on
  2. Now Zhang SAN wants to buy a house and Li Si wants to buy a car. They both entrust an agent to buy it

Create the mediator abstract class

@Data
public abstract class Mediator {

    private String name;

    public Mediator(String name) {
        this.name = name;
    }

    /** * Register *@param colleague
     */
    public abstract void register(Colleague colleague);

    /** * forward *@param colleague
     * @param message
     */
    public abstract void relay(Colleague colleague, String message);
}
Copy the code

Create the Mediator implementation class ConcreteMediator that inherits Mediators

public class ConcreteMediator extends Mediator{

    /** * set */
    private List<Colleague> colleagues = new ArrayList<Colleague>();

    public ConcreteMediator(String name) {
        super(name);
    }


    @Override
    public void register(Colleague colleague) {
        if(! colleagues.contains(colleague)) { colleagues.add(colleague); colleague.setMedium(this); }}@Override
    public void relay(Colleague colleague, String message) {
        for (Colleague ob : colleagues) {
            if(ob ! = colleague) ob.notify(message); }}}Copy the code

Create a colleague abstract class

@Data
public abstract class Colleague {

    protected Mediator mediator;



    public void setMedium(Mediator mediator) {
        this.mediator = mediator;
    }

    /** * notification *@param meaasge
     */
    public abstract void notify(String meaasge);

    /** * send *@param c1
     * @param message
     */
    public abstract void send(Colleague c1, String message);
}

Copy the code

Create Colleague1 class ConcreteColleague1 inherits Colleague

public class ConcreteColleague1 extends Colleague{

    private String name;


    public ConcreteColleague1(String name) {
        this.name = name;
    }

    @Override
    public void notify(String message) {
        System.out.println("Concrete same"+name+"Receive the request." + message);
    }

    @Override
    public void send(Colleague c1, String message) {
        System.out.println("Specific"+name+"Send request:"+ message); mediator.relay(c1, message); }}Copy the code

Create Colleague2 class ConcreteColleague2 inheriting Colleague

public class ConcreteColleague2 extends Colleague{

    private String name;


    public ConcreteColleague2(String name) {
        this.name = name;
    }

    @Override
    public void notify(String message) {
        System.out.println("Concrete same"+name+"Receive the request." + message);
    }

    @Override
    public void send(Colleague c1, String message) {
        System.out.println("Specific"+name+"Send request:"+ message); mediator.relay(c1, message); }}Copy the code

The client

public class Client {

    public static void main(String[] args) {
        Mediator md = new ConcreteMediator("Broker");
        Colleague c1 = new ConcreteColleague1("Zhang");
        md.register(c1);
        Colleague c2 = new ConcreteColleague2("Bill");
        md.register(c2);
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- --");
        c1.send(c1,"Car");
        c2.send(c2,"Buy"); }}Copy the code

Advantages and disadvantages

Advantages:

1) Loose coupling, encapsulating the tight coupling between multiple objects into the intermediary object to achieve loose coupling. 2) Accord with the Demeter principle 3) centralize the interaction between multiple objects in the mediation object. Sending changes requires only modifying mediation objects, providing system flexibility and making colleague objects independent and easy to reuse.Copy the code

Disadvantages:

1) If there are a lot of interactions between colleagues and the situation is complicated, handing them over to the intermediary will make the intermediary become very complicated and difficult to maintain and manage.Copy the code

Applicable scenarios:

1) Complex reference relationships exist between objects in the system, resulting in disorganized structure of the dependency relationship between them and difficulty in reusing the object. 2) You want an intermediate class to encapsulate behavior in multiple classes without generating too many subclasses.Copy the code



Github Demo address: ~ ~ ~ portal ~ ~ ~

Personal blog address: blog.yanxiaolu.cn /