JAVA design patterns - intermediary model

Intermediary model: with a mediation object that encapsulates a set of objects interact intermediary keeping objects explicitly referring to each other, so that it loosely coupled, and can be changed independently of the interaction between them.
FIG intermediary model structure

Mediator abstract method for receiving a definition of the abstract Colleague, specifically defined Colleague mediator entity needs to receive the call Colleague specific method, comprising Abstract Abstract Colleague intermediary properties, particularly Colleague containing specific notification method.

Advantages: reducing the coupling of each Colleague, and makes it possible to independently vary each reuse Colleague classes and specific mediator. Because of how the objects collaborate abstracts mediation as an independent concept and encapsulated in an object, the object of such attention shifts from their own behavior object to the interaction between them up, which is a standing a broader perspective on the system.

Disadvantages: Due to specific mediator centralized control, the complexity of the interaction becomes the complexity of intermediaries, which makes intermediaries are more complex than any one specific Colleague.

/**
 * 抽象中介者
 */
public abstract class Mediator {
    public abstract void Send(String msg,Colleague colleague);  //通知具体同事
}
/**
 * 具体中介者
 */
public class ConcreteMediator extends Mediator {
    private ColleagueA colleagueA;
    private ColleagueB colleagueB;
    public ColleagueA getColleagueA() {
        return colleagueA;
    }
    public void setColleagueA(ColleagueA colleagueA) {
        this.colleagueA = colleagueA;
    }
    public ColleagueB getColleagueB() {
        return colleagueB;
    }
    public void setColleagueB(ColleagueB colleagueB) {
        this.colleagueB = colleagueB;
    }
    @Override
    public void Send(String msg, Colleague colleague) {
        if (colleague instanceof ColleagueA){
            colleagueA.notify(msg);
        }else if (colleague instanceof ColleagueB){
            colleagueB.notify(msg);
        }
    }
}
/**
 * 抽象同事
 */
public abstract class Colleague {
    private Mediator mediator;
    public Mediator getMediator() {
        return mediator;
    }
    public void setMediator(Mediator mediator) {
        this.mediator = mediator;
    }
}
/**
 * 同事A
 */
public class ColleagueA extends Colleague {
    public void send(String msg){
        this.getMediator().Send(msg,this);
    }
    public void notify(String msg){
        System.out.println("同事A得到消息:" + msg);
    }
}
/**
 * 同事B
 */
public class ColleagueB extends Colleague {
    public void send(String msg){
        this.getMediator().Send(msg,this);
    }
    public void notify(String msg){
        System.out.println("同事B得到通知:" + msg);
    }
}
public class Main {
    public static void main(String[] args) {
        ConcreteMediator concreteMediator = new ConcreteMediator();
        ColleagueA colleagueA = new ColleagueA();
        ColleagueB colleagueB = new ColleagueB();
        colleagueA.setMediator(concreteMediator);   //让同事认识中介者
        colleagueB.setMediator(concreteMediator);
        concreteMediator.setColleagueA(colleagueA); //让中介者认识具体同事
        concreteMediator.setColleagueB(colleagueB);
        concreteMediator.Send("第一条通知",colleagueA);
        concreteMediator.Send("第二条通知",colleagueB);
    }
}

Guess you like

Origin blog.csdn.net/qq_36997245/article/details/81878347