首页 > 代码库 > 网状结构的解藕-中介者模式

网状结构的解藕-中介者模式

(#)定义:用一个中介对象封装一系列的对象交互,中介者使各个对象不需要显示的交互作用,从而使其耦合松散,而且可以独立的改变他们的交互

 

(#)适用场景如下:

技术分享

从图一变换成图二以后,系统整体变得很清晰,而且对于中间过程在中介中进行封装,很容易进行扩展、

 

(#)TALK IS CHEEP    场景:如图二

1.首先我定义一下抽象的同事协作类

public class AbstractColleague {
    protected AbstractMediator mediator;

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


}

接下来定义他的三个具体的实现类

/**
 * 采购
 * Author: scw
 * Time: 16-12-28
 */
public class Purchase extends AbstractColleague {

    public Purchase(AbstractMediator mediator) {
        super(mediator);
    }

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

    public void refuseBuyIBM(){
        System.out.println("不再采购IBM");
    }
}

/**
 * 库存
 * Author: scw
 * Time: 16-12-28
 */
public class Stock extends AbstractColleague {
    public Stock(AbstractMediator mediator) {
        super(mediator);
    }

    private static int COMPUTER_NUM=100;

    public void  increase(int num){
        COMPUTER_NUM+=num;
        System.out.println("电脑库存="+COMPUTER_NUM);
    }

    public void decrease(int num){
        COMPUTER_NUM-=num;
        System.out.println("电脑库存="+COMPUTER_NUM);
    }

    public int getStockNumber(){
        return COMPUTER_NUM;
    }

    public void clearStock(){
        System.out.println("清理存货数量为:"+COMPUTER_NUM);
        super.mediator.execute("stock.clear");
    }
}


/**
 * 销售
 * Author: scw
 * Time: 16-12-28
 */
public class Sale extends AbstractColleague {
    public Sale(AbstractMediator mediator) {
        super(mediator);
    }

    public void sellIBMComputer(int num) {
        super.mediator.execute("sale.sell",num);
        System.out.println("销售IBM电脑:"+num);
    }

    public int getSaleStatus(){
        int saleStatus = new Random(System.currentTimeMillis()).nextInt(100);
        System.out.println("IBM电脑销售:"+saleStatus);
        return saleStatus;
    }

    public void offrSale(){
        super.mediator.execute("sale.offSell");
    }
}

2.接下来定义抽象的中介者

/**
 * Author: scw
 * Time: 16-12-28
 */
public abstract class AbstractMediator {
    protected Purchase purchase;
    protected Sale sale;
    protected Stock stock;

    public AbstractMediator() {
        purchase = new Purchase(this);
        sale = new Sale(this);
        stock = new Stock(this);
    }

    public abstract void execute(String str,Object ...objects);
}

注意其中使用的比较巧妙的就是抽象的execute方法,其中使用...,对于参数进行了一个动态的变换

3.剩下的就是具体的具体的中介实现

/**
 * 具体的中介
 * Author: scw
 * Time: 16-12-28ju
 */
public class Mediator extends AbstractMediator {
    @Override
    public void execute(String str, Object... objects) {
        if(StringUtils.contains(str,"purchase.buy")){
            this.buyComputer((Integer) objects[0]);
        }else if (StringUtils.contains(str,"sale.sell")){
            this.sellComputer((Integer) objects[0]);
        }else if(StringUtils.contains(str,"sale.offSell")){
            this.offSell();
        }else{
            this.clearStock();
        }
    }

    private void buyComputer(int num){
        int saleStatus = super.sale.getSaleStatus();
        if(saleStatus>70){
            System.out.println("销售良好采购IBM电脑:"+saleStatus);
        }else {
            num = num/2;
            System.out.println("销售不好采购IBM电脑:"+saleStatus);

        }
        super.stock.increase(num);
    }

    private void sellComputer(int num){
        if(super.stock.getStockNumber()<num){
            super.purchase.buyIBMcomputer(num);
        }
        super.stock.increase(num);
    }

    private void offSell(){
        System.out.println("折价销售电脑"+stock.getStockNumber());
    }

    private void clearStock(){
        super.sale.offrSale();
        super.purchase.refuseBuyIBM();
    }
}

(#)总结,对于上面代码的理解就是:中介持有所有要组合使用的对象,然后当单独调用的一个其中的步奏的时候,回调中介的execute方法,在中介中对于这些不走

进行组装拼接.ok,但是请注意,中介者的缺点是中介者可能膨胀的很大,关系越多那么越复杂,所以注意使用的场景,就比如上图,当各个操作出现一个网状结构的时候,

很适合使用这种模式

 

网状结构的解藕-中介者模式