21、大话设计模式之中介者模式

作用:用一个中介对象,来分装一系列的对象交互,中介者使各对象不需要显式的相互引用,从而使其耦合松散,而且可以独立的改变他们之间的交互

1、中介者模式

1、Colleague抽象同事类

abstract class Colleague {
    protected Mediator mediator;
    //构造方法,得到中介者对象
    public Colleague(Mediator mediator){
        this.mediator = mediator;
    }
}

2、抽象同事类实现

class ConcreteColleague1 extends Colleague {
    public ConcreteColleague1(Mediator mediator) {
        super(mediator);
    }

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

    public void notify(String message) {
        System.out.println("同事1得到信息:" + message);
    }
}

class ConcreteColleague2 extends Colleague {
    public ConcreteColleague2(Mediator mediator) {
        super(mediator);
    }

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

    public void notify(String message){
        System.out.println("同事2得到信息:" + message);
    }
}

3、Mediator抽象中介

abstract class Mediator{
    //定义一个抽象的发送消息方法,得到同事对象和发送信息
    public abstract void send(String message,Colleague colleague);
}

4、抽象中介的实现

class ConcreteMediator extends Mediator{
    private ConcreteColleague1 colleague1;
    private ConcreteColleague2 colleague2;

    public void setColleague1(ConcreteColleague1 value) {
        this.colleague1 = value; 
    }

    public void setColleague2(ConcreteColleague2 value) {
        this.colleague2 = value; 
    }

    public void send(String message, Colleague colleague)
    {
        if (colleague == colleague1) {
            colleague2.notify(message);
        }
        else {
            colleague1.notify(message);
        }
    }
}

5、测试和结果

public class Test {

    public static void main(String[] args) {

        System.out.println("**********************************************");       
        System.out.println("《大话设计模式》代码样例");
        System.out.println(); 

        ConcreteMediator m = new ConcreteMediator();

        ConcreteColleague1 c1 = new ConcreteColleague1(m);
        ConcreteColleague2 c2 = new ConcreteColleague2(m);

        m.setColleague1(c1);
        m.setColleague2(c2);

        c1.send("吃过饭了吗?");
        c2.send("没有呢,你打算请客?");

 
        System.out.println();
        System.out.println("**********************************************");
    }
}

结果

2、中介者模式使用

1、Colleague抽象同事类

abstract class Country {
    protected UnitedNations unitedNations;
    public Country(UnitedNations unitedNations){
        this.unitedNations = unitedNations;
    }
}

2、抽象同事类实现

class USA extends Country {
    public USA(UnitedNations unitedNations) {
        super(unitedNations);
    }

    public void declare(String message) {
        this.unitedNations.declare(message, this);
    }

    public void getMessage(String message) {
        System.out.println("美国获得对方信息:" + message);
    }
}

class Iraq extends Country {
    public Iraq(UnitedNations unitedNations) {
        super(unitedNations);
    }

    public void declare(String message) {
        this.unitedNations.declare(message, this);
    }

    public void getMessage(String message) {
        System.out.println("伊拉克获得对方信息:" + message);
    }
}

3、Mediator抽象中介

//中介者类
abstract class UnitedNations{
    //声明
    public abstract void declare(String message,Country country);
}

4、抽象中介的实现

//联合国安理会
class UnitedNationsSecurityCouncil extends UnitedNations{
    private USA countryUSA;
    private Iraq countryIraq;

    public void setUSA(USA value) {
        this.countryUSA = value; 
    }

    public void setIraq(Iraq value) {
        this.countryIraq = value; 
    }

    public void declare(String message, Country country)
    {
        if (country == this.countryUSA) {
            this.countryIraq.getMessage(message);
        }
        else if (country == this.countryIraq) {
            this.countryUSA.getMessage(message);
        }
    }
}

5、测试和结果

public class Test {

    public static void main(String[] args) {

        System.out.println("**********************************************");       
        System.out.println("《大话设计模式》代码样例");
        System.out.println(); 

        UnitedNationsSecurityCouncil UNSC = new UnitedNationsSecurityCouncil();

        USA c1 = new USA(UNSC);
        Iraq c2 = new Iraq(UNSC);

        UNSC.setUSA(c1);
        UNSC.setIraq(c2);

        c1.declare("不准研制核武器,否则要发动战争!");
        c2.declare("我们没有核武器,也不怕侵略。");


 
        System.out.println();
        System.out.println("**********************************************");
    }
}

结果

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

越来越没意思

你的鼓励将是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值