21 basic introduction of intermediary model

Posted by Danno13 on Thu, 30 Dec 2021 06:03:09 +0100

1. Basic introduction

1) Mediator Pattern, which encapsulates a series of object interactions with a mediation object, makes each object do not need to be explicitly introduced into each other, so that it is loosely coupled and can be used

Change their interaction independently

2) The mediator pattern is a behavioral pattern that makes the code easy to maintain

3) For example, in MVC mode, C (Controller) is the mediator of M (Model mode) and V (View view), and plays the role of intermediary in front and back-end interaction.

2. Schematic class diagram of intermediary mode

>Description of the schematic class diagram - i.e. (roles and responsibilities of the mediator pattern)

1) Mediator {is an abstract mediator, which defines the interface from colleague object to mediator object

2) Collague} is an abstract Colleague class

3) ConcreteMediator is a concrete mediator object that implements abstract methods. It needs to know all specific colleague classes, that is, it is managed in a collection

HashMap, and accept a colleague object message to complete the corresponding task

4) There are many specific colleague classes. Each colleague only knows his own behavior and does not know the behavior (method) of other colleagues,

But they all rely on mediator objects.

3. Application example of intermediary mode - intelligent home management

1) Train of thought analysis and illustration

2) Code implementation

package com.atguigu.mediator.smarthouse;

public abstract class Mediator {
    //Add the object to the mediator to the collection
    public abstract void Register(String colleagueName, Colleague colleague);

    //Receive the message and send it to the specific colleague object
    public abstract void GetMessage(int stateChange, String colleagueName);

    public abstract void SendMessage();
}


package com.atguigu.mediator.smarthouse;

import java.util.HashMap;

//Specific mediator class
public class ConcreteMediator extends Mediator {
    //Collection and put all colleague objects
    private HashMap<String, Colleague> colleagueMap;
    private HashMap<String, String> interMap;

    public ConcreteMediator() {
        colleagueMap = new HashMap<String, Colleague>();
        interMap = new HashMap<String, String>();
    }

    @Override
    public void Register(String colleagueName, Colleague colleague) {
        // TODO Auto-generated method stub
        colleagueMap.put(colleagueName, colleague);

        // TODO Auto-generated method stub

        if (colleague instanceof Alarm) {
            interMap.put("Alarm", colleagueName);
        } else if (colleague instanceof CoffeeMachine) {
            interMap.put("CoffeeMachine", colleagueName);
        } else if (colleague instanceof TV) {
            interMap.put("TV", colleagueName);
        } else if (colleague instanceof Curtains) {
            interMap.put("Curtains", colleagueName);
        }

    }

    //Core methods of specific intermediaries
    //1. Complete the corresponding task according to the received message
    //2. In this method, the intermediary coordinates each specific colleague object to complete the task
    @Override
    public void GetMessage(int stateChange, String colleagueName) {
        // TODO Auto-generated method stub

        //Process messages sent by alarm clock
        if (colleagueMap.get(colleagueName) instanceof Alarm) {
            if (stateChange == 0) {
                ((CoffeeMachine) (colleagueMap.get(interMap
                        .get("CoffeeMachine")))).StartCoffee();
                ((TV) (colleagueMap.get(interMap.get("TV")))).StartTv();
            } else if (stateChange == 1) {
                ((TV) (colleagueMap.get(interMap.get("TV")))).StopTv();
            }

        } else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {
            ((Curtains) (colleagueMap.get(interMap.get("Curtains"))))
                    .UpCurtains();

        } else if (colleagueMap.get(colleagueName) instanceof TV) {//If TV finds a message

        } else if (colleagueMap.get(colleagueName) instanceof Curtains) {
            //If it's a message from a curtain, deal with it here
        }

    }

    @Override
    public void SendMessage() {
        // TODO Auto-generated method stub

    }

}

package com.atguigu.mediator.smarthouse;

//Colleague abstract class
public abstract class Colleague {
    private Mediator mediator;
    public String name;

    public Colleague(Mediator mediator, String name) {

        this.mediator = mediator;
        this.name = name;

    }

    public Mediator GetMediator() {
        return this.mediator;
    }

    public abstract void SendMessage(int stateChange);
}

package com.atguigu.mediator.smarthouse;

public class TV extends Colleague {

    public TV(Mediator mediator, String name) {
        super(mediator, name);
        // TODO Auto-generated constructor stub
        mediator.Register(name, this);
    }

    @Override
    public void SendMessage(int stateChange) {
        // TODO Auto-generated method stub
        this.GetMediator().GetMessage(stateChange, this.name);
    }

    public void StartTv() {
        // TODO Auto-generated method stub
        System.out.println("It's time to StartTv!");
    }

    public void StopTv() {
        // TODO Auto-generated method stub
        System.out.println("StopTv!");
    }
}

package com.atguigu.mediator.smarthouse;

public class Curtains extends Colleague {

    public Curtains(Mediator mediator, String name) {
        super(mediator, name);
        // TODO Auto-generated constructor stub
        mediator.Register(name, this);
    }

    @Override
    public void SendMessage(int stateChange) {
        // TODO Auto-generated method stub
        this.GetMediator().GetMessage(stateChange, this.name);
    }

    public void UpCurtains() {
        System.out.println("I am holding Up Curtains!");
    }

}

package com.atguigu.mediator.smarthouse;

public class CoffeeMachine extends Colleague {

    public CoffeeMachine(Mediator mediator, String name) {
        super(mediator, name);
        // TODO Auto-generated constructor stub
        mediator.Register(name, this);
    }

    @Override
    public void SendMessage(int stateChange) {
        // TODO Auto-generated method stub
        this.GetMediator().GetMessage(stateChange, this.name);
    }

    public void StartCoffee() {
        System.out.println("It's time to startcoffee!");
    }

    public void FinishCoffee() {

        System.out.println("After 5 minutes!");
        System.out.println("Coffee is ok!");
        SendMessage(0);
    }
}

package com.atguigu.mediator.smarthouse;

//Specific colleagues
public class Alarm extends Colleague {

    //constructor 
    public Alarm(Mediator mediator, String name) {
        super(mediator, name);
        // TODO Auto-generated constructor stub
        //When creating the Alarm colleague object, put yourself into the ConcreteMediator object [collection]
        mediator.Register(name, this);
    }

    public void SendAlarm(int stateChange) {
        SendMessage(stateChange);
    }

    @Override
    public void SendMessage(int stateChange) {
        // TODO Auto-generated method stub
        //getMessage of the called mediator object
        this.GetMediator().GetMessage(stateChange, this.name);
    }

}

package com.atguigu.mediator.smarthouse;

public class ClientTest {

    public static void main(String[] args) {
        //Create a mediator object
        Mediator mediator = new ConcreteMediator();
        
        //Create an Alarm and add it to the HashMap of the ConcreteMediator object
        Alarm alarm = new Alarm(mediator, "alarm");
        
        //The CoffeeMachine object is created and added to the HashMap of the ConcreteMediator object
        CoffeeMachine coffeeMachine = new CoffeeMachine(mediator,
                "coffeeMachine");
        
        //Create Curtains and add them to the HashMap of the ConcreteMediator object
        Curtains curtains = new Curtains(mediator, "curtains");
        TV tV = new TV(mediator, "TV");
        
        //Let the alarm clock send a message
        alarm.SendAlarm(0);
        coffeeMachine.FinishCoffee();
        alarm.SendAlarm(1);
    }
}

4. Notes and details of intermediary model

1) When multiple classes are coupled with each other, a mesh structure will be formed. The mediator pattern is used to separate the mesh structure into a star structure for decoupling

2) Reduce the dependency between classes and reduce the coupling, which is in line with the Demeter principle

3) Intermediaries bear more responsibilities. Once the intermediaries have problems, the whole system will be affected

4) If not designed properly, the mediator object itself becomes too complex, which should be paid special attention to in practical use

Topics: Design Pattern