设计模式(8)-中介模式

定义

将多个对象之间的杂乱的调用关系,封装进中介对象中,以减少对象之间的耦合.


示例

进销存系统:
销售员卖货后库存要减少.
销售员从库存获取库存量,发现不够要通知采购购买.
采购员购买后库存要增加.
采购员从销售处获得销售报告,评分高足量采购,评分低半量采购.
仓储员需要增加库存,减少库存.
仓储员发现库存积压时,需要通知采购员不要再采购,通知销售员尽快销售.


传统方式

public class Sale {
    //销售要看是否有库存,库存不够要通知采购购买
    public void sell(int number){
        Stock stock = new Stock();
        Purchase purchase = new Purchase();
        //如果库存不足
        if(stock.getStock() < number){
            purchase.buy(number);
        }
        System.out.println("销售卖出了"+number+"台货!!!");
        stock.decrease(number);
    }

    //尽快卖货
    public void sellFast(){
        System.out.println("尽快卖货!!!");
    }

    //反馈销售情况
    public int report(){
        return new Random().nextInt(100);
    }
}
public class Purchase {
    public void buy(int number){
        Sale sale = new Sale();
        int report = sale.report();
        //销售情况大于50分,情况良好,足量采购
        if(report>50){
            System.out.println("采购购买"+number+"台货!!!");
            Stock stock = new Stock();
            stock.increase(number);
        }else{//销售情况<=50分,情况不好,半数采购
            System.out.println("采购购买"+number/2+"台货!!!");
            Stock stock = new Stock();
            stock.increase(number/2);
        }

    }

    public void dontBuy(){
        System.out.println("不要再购买了!!!");
    }
}
public class Stock {
    public static int STOCK_NUMBER=100;
    //增加库存
    public void increase(int number){
        System.out.println("新增库存"+number+"台!!!");
        STOCK_NUMBER += number;
    }

    //减少库存
    public void decrease(int number){
        System.out.println("减少库存"+number+"台!!!");
        STOCK_NUMBER -= number;
    }

    //获得库存数量
    public int getStock(){
        System.out.println("当前库存为"+STOCK_NUMBER+"台!!!");
        return STOCK_NUMBER;
    }

    //库存压力大时,做清仓处理,通知采购不要再买,通知销售尽快卖
    public void clear(){
        Purchase purchase = new Purchase();
        Sale sale  = new Sale();

        purchase.dontBuy();
        sale.sellFast();
    }
}
    public static void main(String[] args) {
        Sale sale = new Sale();
        sale.sell(120);
    }

输出:

当前库存为100!!!
采购购买60台货!!!
新增库存60!!!
销售卖出了120台货!!!
减少库存120!!!

各个类之间的耦合程度极高,随着角色类的增多,关系将会变得越发复杂.


改造为中介模式


1.抽象中介类

public abstract class AbstractMediator {
    protected Sale sale;
    protected Purchase purchase;
    protected Stock stock;

    //此处的this代表自己的子类实现
    public AbstractMediator(){
        this.sale = new Sale(this);
        this.purchase = new Purchase(this);
        this.stock = new Stock(this);
    }

    //最重要的方法,所有发生对象之间调用的方法都要在此处实现
    public abstract void execute(String str,Object... objects );
}

2.中介实现类

public class Mediator extends AbstractMediator{
    //所有需要对象之间相互调用的方法,在中介里面实现
    @Override
    public void execute(String str, Object... objects) {
        if(str.equals("purchase.buy")){ //采购员购买
            this.buy((Integer)objects[0]);
        }else if(str.equals("sale.sell")){  //销售员卖货
            this.sell((Integer)objects[0]);
        }else if(str.equals("stock.clear")){    //仓储员清仓
            this.clear();
        }
    }

    //购买
    public void buy(int number){
        int report = super.sale.report();
        //销售情况大于50分,情况良好,足量采购
        if(report>50){
            System.out.println("采购购买"+number+"台货!!!");
            super.stock.increase(number);
        }else{//销售情况<=50分,情况不好,半数采购
            System.out.println("采购购买"+number/2+"台货!!!");
            super.stock.increase(number/2);
        }
    }

    //销售
    public void sell(int number){
        //如果库存不足
        if(super.stock.getStock() < number){
            super.purchase.buy(number);
        }
        System.out.println("销售卖出了"+number+"台货!!!");
        super.stock.decrease(number);
    }

    //清仓
    public void clear(){        
        super.purchase.dontBuy();
        super.sale.sellFast();
    }
}

3.抽象同事类

public abstract class AbstractColleague {
    protected AbstractMediator mediator;

    public AbstractColleague(AbstractMediator mediator){
        this.mediator = mediator;
    }
}

4.同事实现类

public class Sale extends AbstractColleague{
    public Sale(AbstractMediator mediator) {
        super(mediator);
    }

    //销售要看是否有库存,库存不够要通知采购购买
    public void sell(int number){
        super.mediator.execute("sale.sell", number);
    }

    //尽快卖货
    public void sellFast(){
        System.out.println("尽快卖货!!!");
    }

    //反馈销售情况
    public int report(){
        return new Random().nextInt(100);
    }
}
public class Purchase extends AbstractColleague{
    public Purchase(AbstractMediator mediator) {
        super(mediator);
    }

    public void buy(int number){
        super.mediator.execute("purchase.buy", number);
    }

    public void dontBuy(){
        System.out.println("不要再购买了!!!");
    }
}
public class Stock extends AbstractColleague{
    public Stock(AbstractMediator mediator) {
        super(mediator);
    }

    public static int STOCK_NUMBER=100;
    //增加库存
    public void increase(int number){
        System.out.println("新增库存"+number+"台!!!");
        STOCK_NUMBER += number;
    }

    //减少库存
    public void decrease(int number){
        System.out.println("减少库存"+number+"台!!!");
        STOCK_NUMBER -= number;
    }

    //获得库存数量
    public int getStock(){
        System.out.println("当前库存为"+STOCK_NUMBER+"台!!!");
        return STOCK_NUMBER;
    }

    //库存压力大时,做清仓处理,通知采购不要再买,通知销售尽快卖
    public void clear(){
        super.mediator.execute("stock.clear");
    }
}

5.运行

    public static void main(String[] args) {
        Sale sale = new Sale(new Mediator());
        sale.sell(120);
    }

输出:

当前库存为100!!!
采购购买60台货!!!
新增库存60!!!
销售卖出了120台货!!!
减少库存120!!!

总结

将对象之间的相互调用封装进中介类,需要用时调用中介的execute方法.

中介模式亦有缺点,当耦合关系复杂时,中介会膨胀很快,所以需要根据实际情况定义多个中介.

阐述中介模式时,诸如聊天室,飞机调度台,网络交换机的例子并不是十分准确,都不如进销存更能说明问题.

你可能感兴趣的:(设计模式)