假设我们要做一个销售系统,里面有采购管理,销售管理,存货管理,之间都相互依赖,我们该怎么做呢,先看图片:
我们从这个示意图上可以看出,三个模块是相互依赖的。我们就以一个终端销售商(以服务最终客户为目标的企业,比如某某超市、某某商店等)为例,采购部门要采购IBM的电脑,它根据以下两个要素来决定采购数量。
销售部门要反馈销售情况,畅销就多采购,滞销就不采购。 即使是畅销产品,库存都有1000台了,每天才卖出去10台,也就不需要再采购了! 库房有货,才能销售。
从以上分析来看,这三个模块都有自己的行为,并且与其他模块之间的行为产生关联, 类似于我们办公室的同事,大家各干各的活,但是彼此之间还是有交叉的,于是彼此之间就 产生紧耦合,也就是一个团队。我们先来实现这个进销存,类图如图:
Purchase:
public class Purchase {
public void buyIBMComputer(int number){
Sale sale = new Sale();
Stock stock = new Stock();
int saleStatus = sale.getSaleStatus();
if (saleStatus > 80){
System.out.println("采购IBM电脑"+number);
stock.increase(number);
}else {
System.out.println("采购IBM电脑"+number/2);
stock.increase(number/2);
}
}
public void refuseBuyIBM(){
System.out.println("不再采购IBM电脑");
}
}
Sale:
public class Sale {
public int getSaleStatus() {
return new Random().nextInt(100);
}
public void offSale() {
Stock stock = new Stock();
System.out.println("折半销售的数量为"+stock.getComputerNumber());
}
public void sellIBMComputer(int number){
Stock stock = new Stock();
Purchase purchase = new Purchase();
//库存不够,需要进货
if (stock.getComputerNumber() < number){
purchase.buyIBMComputer(number);
}
stock.decrease(number);
}
}
Stock:
public class Stock {
private static int COMPUTER_NUMBER = 100;
public void increase(int number) {
COMPUTER_NUMBER = COMPUTER_NUMBER + number;
}
public void decrease(int number) {
COMPUTER_NUMBER = COMPUTER_NUMBER - number;
}
public int getComputerNumber(){
return COMPUTER_NUMBER;
}
public void clearStock(){
Purchase purchase = new Purchase();
Sale sale = new Sale();
System.out.println("清理存货数量"+COMPUTER_NUMBER);
//折半销售
sale.offSale();
purchase.refuseBuyIBM();
}
}
场景类:
public class Client {
public static void main(String[] args) {
System.out.println("采购人员采购电脑......");
Purchase purchase = new Purchase();
purchase.buyIBMComputer(100);
System.out.println("销售人员销售电脑......");
Sale sale = new Sale();
sale.sellIBMComputer(1);
System.out.println("库房管理清库.....");
Stock stock = new Stock();
stock.clearStock();
}
}
结果如下:
运行结果也是我们期望的,三个不同类型的参与者完成了各自的活动。你有没有发现这 三个类是彼此关联的?每个类都与其他两个类产生了关联关系。迪米特法则认为“每个类只 和朋友类交流”,这个朋友类并非越多越好,朋友类越多,耦合性越大,要想修改一个就得修改一片,这不是面向对象设计所期望的,况且这还是仅三个模块的情况,属于比较简单的一个小项目。我们扩展一下,如图:
这是一个蜘蛛网的结构,别说是编写程序了,就是给人看估计也能让一大批人昏倒!每 个对象都需要和其他几个对象交流,对象越多,每个对象要交流的成本也就越大了,只是维 护这些对象的交流就能让一大批程序员望而却步,那我们该如何修改呢?
类图如下:
抽象中介者:
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... 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 clearStock() {
super.sale.offSale();
//要求采购人员不要采购
super.purchase.refuseBuyIBM();
}
private void offSell() {
System.out.println("折半销售的数量为"+super.stock.getComputerNumber());
}
private void sellComputer(Integer number) {
//库存不够,需要进货
if (super.stock.getComputerNumber() < number){
super.purchase.buyIBMComputer(number);
}
super.stock.decrease(number);
}
private void buyComputer(Integer number) {
int saleStatus = super.sale.getSaleStatus();
if (saleStatus > 80){
System.out.println("采购IBM电脑"+number);
stock.increase(number);
}else {
System.out.println("采购IBM电脑"+number/2);
stock.increase(number/2);
}
}
}
抽象同事类:
public abstract class AbstractColleague {
protected AbstractMediator abstractMediator;
public AbstractColleague(AbstractMediator abstractMediator){
this.abstractMediator = abstractMediator;
}
}
修改后的采购管理:
public class Purchase extends AbstractColleague{
public Purchase(AbstractMediator abstractMediator){
super(abstractMediator);
}
public void buyIBMComputer(int number){
super.abstractMediator.execute("purchase.buy",number);
}
public void refuseBuyIBM(){
System.out.println("不再采购IBM电脑");
}
}
修改后的库存管理:
public class Stock extends AbstractColleague{
private static int COMPUTER_NUMBER = 100;
public Stock(AbstractMediator abstractMediator) {
super(abstractMediator);
}
public void increase(int number) {
COMPUTER_NUMBER = COMPUTER_NUMBER + number;
}
public void decrease(int number) {
COMPUTER_NUMBER = COMPUTER_NUMBER - number;
}
public int getComputerNumber(){
return COMPUTER_NUMBER;
}
public void clearStock(){
System.out.println("清理存货数量"+COMPUTER_NUMBER);
super.abstractMediator.execute("stock.clear");
}
}
修改后的销售管理:
public class Sale extends AbstractColleague{
public Sale(AbstractMediator abstractMediator) {
super(abstractMediator);
}
public int getSaleStatus() {
return new Random().nextInt(1000);
}
public void offSale() {
super.abstractMediator.execute("sale.offsell");
}
public void sellIBMComputer(int number){
super.abstractMediator.execute("sale.sell",number);
System.out.println("销售IBM电脑"+number);
}
}
修改后的场景类:
public class Client {
public static void main(String[] args) {
Mediator 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();
}
}
运行结果:
在场景类中增加了一个中介者,然后分别传递到三个同事类中,三个类都具有相同的特
性:只负责处理自己的活动(行为),与自己无关的活动就丢给中介者处理,程序运行的结
果是相同的。从项目设计上来看,加入了中介者,设计结构清晰了很多,而且类间的耦合性
大大减少,代码质量也有了很大的提升。
中介者模式的定义:
中介者模式的定义为: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.(用一个中介对象封装一系列的对象 交互,中介者使各对象不需要显示地相互作用,从而使其耦合松散,而且可以独立地改变它 们之间的交互。)
通用类图如下:
优点:中介者模式的优点就是减少类间的依赖,把原有的一对多的依赖变成了一对一的依赖, 同事类只依赖中介者,减少了依赖,当然同时也降低了类间的耦合。
缺点:中介者模式的缺点就是中介者会膨胀得很大,而且逻辑复杂,原本N个对象直接的相互依赖关系转换为中介者和同事类的依赖关系,同事类越多,中介者的逻辑就越复杂。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)