Design Pattern Notes 19 - Mediator Pattern (mediator)

Smart Home Projects:

1) Smart home includes various devices, alarm clock, coffee machine, TV, curtains, etc.

2) When the owner wants to watch TV, all devices can work together to automatically complete the preparations for watching TV. For example, the process is: the alarm rings -> the coffee machine starts making coffee -> the curtains automatically fall -> the TV starts playing

 

 

Traditional way of problem analysis

1) When each electrical object has multiple state changes, the calling relationship between them will be more complicated

2) Each electrical object is connected with each other, there is me in you, and you in me, which is not conducive to loose coupling.

3) Messages (parameters) transmitted between electrical objects are easy to be confused

4) When the system adds a new electrical object, or when the execution process changes, the maintainability and scalability of the code are not ideal Consider the intermediary model

 

basic introduction

1) Mediator Pattern, using a mediator object to encapsulate a series of object interactions. Mediators make objects loosely coupled by eliminating the need for explicit references to each other, and their interactions can be changed independently

2) The intermediary mode is a behavioral mode, which makes the code easy to maintain

3) For example in the MVC mode, C (Controller controller) is the intermediary between M (Model model) and V (View view), and plays the role of a middleman in the front-end and back-end interactions.

 

Principle class diagram of intermediary mode

Explanation of the principle class diagram - that is (the role and responsibility of the intermediary model)

1) Mediator is an abstract intermediary, which defines the interface from colleague objects to intermediary objects

2) Colleague is an abstract colleague class

3) ConcreteMediator The specific intermediary object implements the abstract method, he needs to know all the specific colleague classes

 

Core code:

// Create an Alarm and add it to the HashMap of the ConcreteMediator object
        Alarm alarm = new Alarm(mediator, "alarm");

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




The core lies in the register method of the mediator:
    @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);
        }

    }

Once the alarm is operated:
//Let the alarm clock send a message
        alarm.SendAlarm(0);

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

        //Process the message from the 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 found the message

        } else if (colleagueMap.get(colleagueName) instanceof Curtains) {             //If the message is sent by the curtain, it will be processed here...         }

    }
 

After sending the message to the mediator, the mediator calls other classes for related processing

 

Notes and Details of the Mediator Pattern

1) Multiple classes are coupled with each other to form a network structure, and the intermediary mode is used to separate the network structure into a star structure for decoupling

2) Reduce inter-class dependencies, reduce coupling, and comply with the Dimit principle

3) The intermediary assumes more responsibilities. Once there is a problem with the intermediary, the entire system will be affected

4) If the design is not proper, the intermediary object itself will become too complicated, so special attention should be paid to this point in actual use

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_22059611/article/details/103296766