一、理论
Define an object that encapsulates how a set of objects interact.Mediator promotes loose coupling by keeping objects from referring to each other explicitly,and it lets you vary their interaction independently.用一个中介对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
- Mediator(中介者、仲裁者)
Mediator角色负责定义与Colleague角色进行通信和做出决定的接口(API )。 - ConcreteMediator(具体的中介者、仲裁者)
ConcreteMediator角色负责实现 Mediator 角色的接口(API ),负责实际做出决定。 - Colleague(同事)
Colleague角色负责定义与Mediator角色进行通信的接口(API )。 - ConcreteColleague(具体的同事)
ConcreteColleague角色负责实现Colleague角色的接口(API )。会有很多, 每个同事只知道自己的行为, 而不了解其他同事类的行为(方法), 但是他们都依赖中介者对象。
二、代码
假设有采购、销售、库存三个系统,三个系统之前是彼此有影响的。此时,采用中介者模式,使三者之间互相解耦,全部依赖中介者对象。
中介者:
public abstract class AbstractMediator {
protected SalesColleague salesColleague;
protected PurchaseColleague purchaseColleague;
protected StockColleague stockColleague;
public AbstractMediator() {
salesColleague = new SalesColleague(this);
purchaseColleague = new PurchaseColleague(this);
stockColleague = new StockColleague(this);
}
public abstract void execute(String str, Object... args);
}
具体的中介者:
public class Mediator extends AbstractMediator {
@Override
public void execute(String str, Object[] args) {
if ("sale.sell".equals(str)) {
this.sell((int) args[0]);
} else if ("sale.offSell".equals(str)) {
this.offSell();
} else if ("purchase.buy".equals(str)) {
buy((int) args[0]);
} else if ("stock.clear".equals(str)) {
clearStock();
}
}
private void clearStock() {
super.salesColleague.offSell();
super.purchaseColleague.refuseBuy();
}
private void offSell() {
System.out.println("折价销售商品:" + stockColleague.getStockNum());
}
private void sell(int number) {
System.out.println("销售商品:" + number);
super.stockColleague.decrease(number);
}
private void buy(int number) {
int saleStatus = super.salesColleague.getSaleStatus();
super.stockColleague.decrease(saleStatus);
if (saleStatus > 80) {
System.out.println("销售很好,补充库存,采购商品:" + number);
super.stockColleague.increase(number);
} else {
int buyNum = number / 2;
System.out.println("销售不好,采购商品:" + buyNum);
super.stockColleague.increase(buyNum);
}
}
}
同事:
public abstract class AbstractColleague {
protected AbstractMediator mediator;
public AbstractColleague(AbstractMediator mediator) {
this.mediator = mediator;
}
}
具体的同事-采购、销售、库存:
public class PurchaseColleague extends AbstractColleague {
public PurchaseColleague(AbstractMediator mediator) {
super(mediator);
}
public void buyProduct(int number) {
super.mediator.execute("purchase.buy", number);
}
public void refuseBuy() {
System.out.println("暂时不采购商品");
}
}
public class SalesColleague extends AbstractColleague {
public SalesColleague(AbstractMediator mediator) {
super(mediator);
}
/**
* 正常销售
*
* @param number
*/
public void sell(int number) {
super.mediator.execute("sale.sell", number);
System.out.println("销售商品:" + number);
}
/**
* 反馈销售情况,0-100表示畅销程度
*
* @return
*/
public int getSaleStatus() {
int saleStatus = new Random().nextInt(100);
System.out.println("商品的销售情况:" + saleStatus);
return saleStatus;
}
/**
* 打折处理
*
* @return
*/
public void offSell() {
super.mediator.execute("sale.offSell");
}
}
public class StockColleague extends AbstractColleague {
private static int STOCK_NUM = 100;
public StockColleague(AbstractMediator mediator) {
super(mediator);
}
public int getStockNum() {
return STOCK_NUM;
}
public void increase(int number) {
STOCK_NUM += number;
System.out.println("增加库存:" + number + " ,现库存:" + STOCK_NUM);
}
public void decrease(int number) {
STOCK_NUM -= number;
System.out.println("减少库存:" + number + " ,现库存:" + STOCK_NUM);
}
public void clearStock() {
System.out.println("清仓库存:" + STOCK_NUM);
super.mediator.execute("stock.clear");
}
}
客户端:
public class Client {
public static void main(String[] args) {
AbstractMediator mediator = new Mediator();
System.out.println("***采购人员采购商品***");
PurchaseColleague purchaseColleague = new PurchaseColleague(mediator);
purchaseColleague.buyProduct(100);
System.out.println("***销售人员销售商品***");
SalesColleague salesColleague = new SalesColleague(mediator);
salesColleague.sell(1);
System.out.println("***库房清理库存***");
StockColleague stockColleague = new StockColleague(mediator);
stockColleague.clearStock();
}
}
三、总结
- 多个类相互耦合,会形成网状结构, 使用中介者模式将网状结构分离为星型结构,进行解耦
- 减少类间依赖,降低了耦合,符合迪米特原则
- 中介者承担了较多的责任,一旦中介者出现了问题,整个系统就会受到影响
- 如果设计不当,中介者对象本身变得过于复杂,这点在实际使用时,要特别注意