大话设计模式-中介者模式

UML

抽象中介者类 代替某个对象去做某些事情

/** * @ClassName: Mediator * @Author: Leo * @Description: 抽象中介者类 代替某个对象去做某些事情 * @Date: 2019/5/21 10:52 */
public abstract class Mediator {
    /** * 定义一个抽象的发送消息的方法,得到同事对象和发送消息 * * @param message * @param colleague */
    public abstract void send(String message, Colleague colleague);
}

具体的中介者类

/** * @ClassName: ConcreteMediator * @Author: Leo * @Description: 具体的中介者类 * @Date: 2019/5/21 10:57 */
public class ConcreteMediator extends Mediator {
    /** * 中介者需要知道所有的同事对象 */
    private ColleagueImpl1 colleagueImpl1;
    private ColleagueImpl2 colleagueImpl2;

    public void setColleagueImpl1(ColleagueImpl1 colleagueImpl1) {
        this.colleagueImpl1 = colleagueImpl1;
    }

    public void setColleagueImpl2(ColleagueImpl2 colleagueImpl2) {
        this.colleagueImpl2 = colleagueImpl2;
    }

    @Override
    public void send(String message, Colleague colleague) {
        if (colleague instanceof ColleagueImpl1) {
            colleagueImpl2.receiveMessage(message);
        } else if (colleague instanceof ColleagueImpl2) {
            colleagueImpl1.receiveMessage(message);
        }
    }
}

抽象同事类

/** * @ClassName: Colleague * @Author: Leo * @Description: 抽象同事类 * @Date: 2019/5/21 10:53 */
public abstract class Colleague {
    protected Mediator mediator;

    public Colleague(Mediator mediator) {
        this.mediator = mediator;
    }

    /** * 发送消息 * @param message */
    public abstract void send(String message);

    /** * 接收消息 * @param message */
    public abstract void receiveMessage(String message);


}

具体的同事对象1

/** * @ClassName: ColleagueImpl1 * @Author: Leo * @Description: 具体的同事对象1 * @Date: 2019/5/21 10:57 */
public class ColleagueImpl1 extends Colleague {
    public ColleagueImpl1(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void send(String message) {
        mediator.send(message, this);
    }

    @Override
    public void receiveMessage(String message) {
        System.out.println("具体的同事对象1接收到消息--->" + message);
    }
}

具体的同事对象2

/** * @ClassName: ColleagueImpl2 * @Author: Leo * @Description: 具体的同事对象2 * @Date: 2019/5/21 10:57 */
public class ColleagueImpl2 extends Colleague {
    public ColleagueImpl2(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void send(String message) {
        mediator.send(message, this);
    }

    @Override
    public void receiveMessage(String message) {
        System.out.println("具体的同事对象2接收到消息--->" + message);
    }
}

测试类

/** * @ClassName: Main * @Author: Leo * @Description: 测试类 * @Date: 2019/5/21 10:51 */
public class Main {
    public static void main(String[] args) {
        ConcreteMediator concreteMediator = new ConcreteMediator();

        ColleagueImpl1 colleagueImpl1 = new ColleagueImpl1(concreteMediator);
        ColleagueImpl2 colleagueImpl2 = new ColleagueImpl2(concreteMediator);

        concreteMediator.setColleagueImpl1(colleagueImpl1);
        concreteMediator.setColleagueImpl2(colleagueImpl2);

        colleagueImpl1.send("你好,我是同事1~");
        colleagueImpl2.send("你也好啊,我是同事2~");

    }
}

运行结果

具体的同事对象2接收到消息--->你好,我是同事1~
具体的同事对象1接收到消息--->你也好啊,我是同事2~
全部评论

相关推荐

offer多多的六边形战士很无语:看了你的博客,感觉挺不错的,可以把你的访问量和粉丝数在简历里提一下,闪光点(仅个人意见)
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务