码迷,mamicode.com
首页 > 其他好文 > 详细

中介者模式

时间:2016-05-13 08:13:26      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

中介者模式定义:
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects
from referring to each other explicity, and it lets you vary their interaction independently
用一个中介对象封装一系列的对象交互,中介者使各对对象不需要显示相互作用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

一般遇到这种多个对象相互依赖的关系是,可以考虑使用中介者模式

技术分享技术分享

技术分享

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);    
}

public class Mediator extends AbstractMediator{
    public void execute(String str,Object... object){
        if(str.equals("purchase.buy")){
            this.buyComputer((Integer)object[0]);
        }else if(str.equals("sale.sell")){
            this.sellComputer((Integer)objects[0]);
        }else if(str.equals(stock.clear)){
            this.clearStock();
        }
    }
    
    private void buyComputer(int number){
        int saleStatus=super.sale.getSaleStatus();
        if(saleStatus>80){
            System.out.println("采购IBM电脑:"+number+"台");
            super.stock.increase(number);
        }else{
            int buyComputer=number/2;
            System.out.println("采购IBM电脑:"+buyComputer+"台");
    }
    
    private void sellComputer(int number){
        if(super.stock.getStockNumber()<number){
            super.purchase.buyIBMComputer(number);
        }
        super.stock.decrease(number);
    }
    
    private void offSell(){
        System.out.println("折价销售IBM电脑"+stock.getStockNubmer()+"台");
    }
    
    private void clearStock(){
        super.sale.offSale();
        super.purchase.refuseBuyIBM();
    }
}

//抽象同事类
public abstract class AbstractColleague{
    protected AbstractMediator mediator;
    public AbstractColleague(AbstractMediator _mediator){
        this.mediator=_mediator;
    }
}

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电脑");
    }    
}

public class Stock extends AbstractColleague{
    public Stock(AbstractMediator _mediator){
        super(_mediator);
    }
    private static int COMPUTER_NUMBER=100;
    public void increase(int number){
        COMPUTER_NUMBER=COMPUTER_NUMBER+number;
        System.out.println("库存数量为:"+COMPUTER_NUMBER);
    }
    public void decrease(int number){
        COMPUTER_NUMBER=COMPUTER_NUMBER-number;
        System.out.println("库存数量为:"+COMPUTER_NUMBER);
    }
    public int getStockNumber(){
        return COMPUTER_NUMBER;    
    }
    public void clearStock(){
        super.mediator.execute("stock.clear");
    }
}

public class Sale extends AbstractColleague{
    public Sale(AbstractMediator _mediator){
        super(_mediator);
    }
    public void sellIBMComputer(int number){
        super.mediator.execute("sale.sell",number);
        System.out.println("销售IBM电脑"+number+"台"):
    }
    public int getSaleStatus(){
        Random rand=new Random(System.currentTimeMillis());
        int saleStatus=rand.nextInt(100);
        System.out.println("IBM电脑的销售情况为:"+saleStatus);
        return saleStatus;
    }
    public void offSale(){
        super.mediator.execute("sale.offSell");
    }
}

 

在如下情况下可以尝试使用中介者模式:
1.N个对象产生了相互依赖关系
2.多个对象有依赖关系,但是依赖行为尚不确定或者有发生改变的可能,在这种情况下一般建议采用中介者模式,降低变更引起的风险扩散
3.产品开发。一个明显的例子就是MVC框架,把中介者模式应用到产品中,可以提升产品的性能和扩展性,但是对于项目开发就未必,因为
项目是以交付投产为目标,而产品则是以稳定,高效,扩展为宗旨

中介者模式

标签:

原文地址:http://www.cnblogs.com/liaojie970/p/5485050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!