设计模式_中介者模式
定义
Define an object that encapsulates how a set of objects interact. Mediator promptes loose
coupling by keeping objects from referring to each other explicitly,and it lets you vary their interaction independently.(用一个中介对象封装一系列的对象交互。中介者使各对象不须要显示地相互作用。从而使其耦合松散,并且能够独立地改变它们之间的交互)
不认识的单词
encapsulates:
a set of :一系列
interact:
loose:
coupling: 耦合
referring :
explicitly:明白地,明白地
vary:
举例
比如以下的图
採购管理,销售管理,存货管理.....多个模块之间有交流,假设须要维护改动什么的。谁愿意去当那个程序员?
依据网络拓扑结构,有三种类型。总线型。环形,星型。
依据这个设计出例如以下
类图
分析类图:三个子模块继承AbstractColleague,Mediator继承AbstractMediator,其父类有三个子模块的实例。另一个统一处理的execute方法()。完毕特定的任务。须要三个子模块中独立的方法。
public abstract class AbstractColleague { protected AbstractMediator mediator; //为何是protected:仅仅对子类公开 public AbstractColleague(AbstractMediator mediator){ this.mediator=mediator; } }
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 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 Sale extends AbstractColleague { public Sale(AbstractMediator mediator) { super(mediator); } public void sellIBMComputer(int number){ super.mediator.execute("sale.sell",number); System.out.println("销售电脑:"+number+"台"); } //反馈,0~100变化 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"); } }
public class Stock extends AbstractColleague { public Stock(AbstractMediator mediator) { super(mediator); } private static int COMPUTER_NUMBER=100; public void increase(int number){ COMPUTER_NUMBER+=number; System.out.println("库存数量:"+COMPUTER_NUMBER); } public void decrease(int number){ COMPUTER_NUMBER-=number; System.out.println("库存数量:"+COMPUTER_NUMBER); } public int getStockNumber(){ return COMPUTER_NUMBER; } public void clearStock(){ System.out.println("清理存货数量:"+COMPUTER_NUMBER); super.mediator.execute("stock.clear"); } }
中介者,处理复杂的业务。调用三个同事中一个或多个方法。
public class Mediator extends AbstractMediator { @Override public void execute(String str, Object... objects) { if(str.equals("purchase.buy")){ //採购电脑 this.buyComputer((Integer)objects[0]); }else if(str.equals("sale.sell")){ //销售电脑 this.sellComputer((Integer)objects[0]); }else if(str.equals("sale.offsell")){ //折价销售 this.offSell(); }else if(str.equals("stock.clear")){ //清仓处理 this.clearStock(); } } private void buyComputer(Integer number) { int saleStatus=super.sale.getSaleStatus(); if(saleStatus>80){ //销售状态好 System.out.println("採购IBM电脑:"+number+"台"); super.stock.increase(number); }else{ //销售状态不好 int buyNumber=number/2; System.out.println("採购IBM电脑:"+buyNumber+"台"); } } private void sellComputer(Integer number) { if(super.stock.getStockNumber()<number){ //库存不足不够销售 super.purchase.buyIBMcomputer(number); } super.stock.decrease(number); } //折价销售 private void offSell() { System.out.println("折价销售IBM电脑"+stock.getStockNumber()+"台"); } //清仓处理 private void clearStock() { super.sale.offSale(); super.purchase.refuseBuyIBM(); } }
測试
public static void main(String[] args) { AbstractMediator mediator=new Mediator(); //一个中介者 System.out.println("------------採购人员採购电脑--------------"); Purchase purchase=new Purchase(mediator); //查看其抽象类的构造方法就知道 purchase.buyIBMcomputer(100); System.out.println("-------------销售人员销售电脑-------------"); Sale sale=new Sale(mediator); sale.sellIBMComputer(1); System.out.println("------------库房管理人员清库处理--------------"); Stock stock=new Stock(mediator); stock.clearStock(); }
说明
public abstract void execute(String str,Object...objects); 可变參数数组,详细可訪问 http://blog.csdn.net/testcs_dn/article/details/38920323
悟:多个模块之间有耦合,须要交流时,业务写的非常杂非常乱,这时候须要一个中介来管理麻烦事。
通用写法
public abstract class Mediator { //定义同事类 protected ConcreteColleague1 c1; protected ConcreteColleague2 c2; //通过getter/setter方法把同事类注入进来 public ConcreteColleague1 getC1() { return c1; } public void setC1(ConcreteColleague1 c1) { this.c1 = c1; } public ConcreteColleague2 getC2() { return c2; } public void setC2(ConcreteColleague2 c2) { this.c2 = c2; } //中介者模式的业务逻辑 public abstract void doSomething1(); public abstract void doSomething2(); }
public abstract class Colleague { protected Mediator mediator; public Colleague(Mediator _mediator){ this.mediator = _mediator; } }
public class ConcreteMediator extends Mediator { @Override public void doSomething1() { //调用同事类的方法,仅仅要是public方法都能够调用 super.c1.selfMethod1(); super.c2.selfMethod2(); } public void doSomething2() { super.c1.selfMethod1(); super.c2.selfMethod2(); } }
public class ConcreteColleague1 extends Colleague { //通过构造函数传递中介者 public ConcreteColleague1(Mediator _mediator){ super(_mediator); } //自有方法 self-method public void selfMethod1(){ //处理自己的业务逻辑 } //依赖方法 dep-method public void depMethod1(){ //处理自己的业务逻辑 //自己不能处理的业务逻辑,托付给中介者处理 super.mediator.doSomething1(); } }
各个同事单一的任务,各干各的事,而中介者ConcreteMediator 这个项目经理须要调动各部分完毕一件事。
长处
降低了耦合,降低了依赖
缺点
同事类越多。越复杂,适合小系统
使用场景
在类图中出现了蜘蛛网网状结构
实际应用举例
机场调度中心
MVC框架: C(Controller) 就是一个中介者,前端控制器,就是把M(Model,业务逻辑)和V(Viw,视图)隔离开
中介服务
同事 : abstract
兄弟 : interface
这个比喻有点逗
我是菜鸟,我在路上。