17. The intermediary model

Intermediary model

First, the intermediary model (Mediator Pattern)

1. Basic Introduction

1) with an intermediary object that encapsulates a series of object interaction. Mediator the respective objects do not explicitly referring to each other, so that it loosely coupled, and can be independently varied interaction therebetween

2) intermediary model belongs to the type of model, making the code easier to maintain

3) such as MVC pattern, C (Controller Controller) is M (Model model) and V (View view) of the intermediary, acts as an intermediary when the rear end of the front interacting

2. The principle of class diagram

1) Mediator Mediator abstract is concerned, the interface colleagues object to the intermediary objects

2) Colleague colleague abstract class

3) ConcreteMediator specific intermediary object that implements the abstract method, he needs to know all of the specific type of co-workers, that is a set of management HashMap, and accept a colleague object message, to accomplish the task

4) ConcreteColleague specific class colleagues, co-workers each only know their own behavior, rather than to understand the behavior (methods) class of other colleagues, we rely on an intermediary target

Second, application examples

Example 1. Requirements

Smart home project:
1) smart home includes a variety of devices, alarm clock, coffee machine, TV, curtains, etc.

2) When the owner to watch TV, individual devices can work together, watch TV automatically preparations, such procedures: the alarm sounds> Coffee machine starts to make coffee -> Curtain automatically fall> TV start playing

2. Analysis of ideas

3. code implementation

public class ClientTest {
    public static void main(String[] args) {
        //创建一个中介者对象
        Mediator mediator = new ConcreteMediator();
        
        //创建Alarm 并且加入到  ConcreteMediator 对象的HashMap
        Alarm alarm = new Alarm(mediator, "alarm");
        
        //创建了CoffeeMachine 对象,并  且加入到  ConcreteMediator 对象的HashMap
        CoffeeMachine coffeeMachine = new CoffeeMachine(mediator,
                "coffeeMachine");
        
        //创建 Curtains , 并  且加入到  ConcreteMediator 对象的HashMap
        Curtains curtains = new Curtains(mediator, "curtains");
        TV tV = new TV(mediator, "TV");
        
        //让闹钟发出消息
        alarm.SendAlarm(0);
        coffeeMachine.FinishCoffee();
        alarm.SendAlarm(1);
    }
}
//同事抽象类
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);
}

//具体的同事类
public class Alarm extends Colleague {

    //构造器
    public Alarm(Mediator mediator, String name) {
        super(mediator, name);
        //在创建Alarm 同事对象时,将自己放入到ConcreteMediator 对象中[集合]
        mediator.Register(name, this);
    }

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

    @Override
    public void SendMessage(int stateChange) {
        //调用的中介者对象的getMessage
        this.GetMediator().GetMessage(stateChange, this.name);
    }
}
// 抽象中介者
public abstract class Mediator {
    //将给中介者对象,加入到集合中
    public abstract void Register(String colleagueName, Colleague colleague);

    //接收消息, 具体的同事对象发出
    public abstract void GetMessage(int stateChange, String colleagueName);

    public abstract void SendMessage();
}
//具体的中介者类
public class ConcreteMediator extends Mediator {
    //集合,放入所有的同事对象
    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) {
        colleagueMap.put(colleagueName, colleague);

        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);
        }
    }

    //具体中介者的核心方法
    //1. 根据得到消息,完成对应任务
    //2. 中介者在这个方法,协调各个具体的同事对象,完成任务
    @Override
    public void GetMessage(int stateChange, String colleagueName) {
        //处理闹钟发出的消息
        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) {
            //如果TV发现消息
        } else if (colleagueMap.get(colleagueName) instanceof Curtains) {
            //如果是以窗帘发出的消息,这里处理...
        }
    }

    @Override
    public void SendMessage() {
    }
}

Third, the intermediary model Notes

1) Mediator assumed more responsibility, once the mediator there is a problem, the whole system will be affected.

2) a plurality of classes coupled to each other, will form a network structure, the mesh structure intermediary model is separated into the star structure, decoupled.

Guess you like

Origin www.cnblogs.com/chao-zjj/p/11334971.html