中介者模式(非中介者场景)
即没有统一的管理者,需要与其他类交流时就new一个
场景
- 三个类:采购、销售、库存
- 采购商品时,联系销售看看的销售情况好不好,再决定采购的数量
- 销售负责反馈销售情况,还有折扣处理
- 库存负责管理库存数量,库存压力大时通知采购人员不要采购、销售人员尽快销售等
非中介者场景
采购类
/*** 采购管理*/
public class Purchase {// 采购IBM电脑public void butIBMComputer(int number){// 访问库存Stock stock = new Stock();// 访问销售Sale sale = new Sale();// 电脑销售情况int saleStatus = sale.getSaleStatus();// 销售情况良好if (saleStatus > 80) {System.out.println("采购IBM电脑:" + number + "台");stock.increase(number);} else {// 销售情况不好int buyNumber = number / 2;System.out.println("采购IBM电脑:" + buyNumber + "台");}}// 不再采购IBM电脑public void refuseBuyIBM(){System.out.println("不再采购IBM电脑");}
}
销售类
/*** 销售类*/
public class Sale {// 销售IBM电脑public void sellIBMComputer(int number) {// 访问库存Stock stock = new Stock();// 访问采购Purchase purchase = new Purchase();// 库存不足if (stock.getStockNumber() < number) {purchase.butIBMComputer(number);}System.out.println("销售了" + number + "台IBM电脑");stock.decrease(number);}// 反馈销售情况,0~100之间的变化,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(){// 库房有多少卖多少Stock stock = new Stock();System.out.println("折扣销售IBM电脑" + stock.getStockNumber() + "台");}
}
库存类
/*** 库存类*/
public class Stock {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(){Purchase purchase = new Purchase();Sale sale = new Sale();System.out.println("清理存贷数量为:" + COMPUTER_NUMBER);// 要求折价销售sale.offSale();// 要求采购人员不要采购purchase.refuseBuyIBM();}
}
入口类
public class NotMediatorMain {public static void main(String[] args) {// 采购人员采购电脑System.out.println("-----采购人员采购电脑-----");Purchase purchase = new Purchase();purchase.butIBMComputer(100);System.out.println("-----销售人员销售电脑-----");Sale sale = new Sale();sale.sellIBMComputer(1);System.out.println("-----库存管理人员清库处理-----");Stock stock = new Stock();stock.clearStock();}
}
中介者模式(中介者场景)
每个类只负责本职工作,不属于本类的范畴都交给中介。
简而言之,就是类的方法中存在需要new的、不属于本类管理范畴的,都交给中介去做,当然,这个中介是牛马
场景
- 三个类:采购、销售、库存
- 采购商品时,联系销售看看的销售情况好不好,再决定采购的数量
- 销售负责反馈销售情况,还有折扣处理
- 库存负责管理库存数量,库存压力大时通知采购人员不要采购、销售人员尽快销售等
中介者场景
抽象中介者类
/*** 抽象中介者*/
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 {@Overridepublic void execute(String str, Object... objects) {if (OptionConstant.PURCHASE_BUY.equals(str)) {this.buyComputer((Integer) objects[0]);} else if(OptionConstant.SALE_SELL.equals(str)) {this.sellComputer((Integer) objects[0]);} else if(OptionConstant.SALE_OFF_SELL.equals(str)){this.offSell();} else if (OptionConstant.STOCK_CLEAR.equals(str)){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 {System.out.println("采购IBM电脑:" + (number / 2) + "台");}}// 销售电脑private void sellComputer(int number) {// 库存不足,要采购if (super.stock.getStockNumber() < number) {super.purchase.butIBMComputer(number);}super.stock.decrease(number);}// 折价销售电脑private void offSell() {System.out.println("折价销售IBM电脑:" + stock.getStockNumber() + "台");}// 清仓处理private void clearStock(){// 要求清仓销售super.sale.offSale();// 要求采购人员不要采购super.purchase.refuseBuyIBM();}
}
静态资源类OptionConstant
/*** 静态资源类,描述同事类中的行为*/
public class OptionConstant {public static final String PURCHASE_BUY = "purchase.buy";public static final String SALE_SELL = "sale.sell";public static final String SALE_OFF_SELL = "sale.offSell";public static final String STOCK_CLEAR = "stock.clear";
}
抽象同事类
/*** 抽象同事类*/
public abstract class AbstractColleague {protected AbstractMediator mediator;public AbstractColleague(AbstractMediator mediator){this.mediator = mediator;}protected AbstractColleague() {}
}
采购类(修改后)
/*** 采购管理*/
public class Purchase extends AbstractColleague {public Purchase(AbstractMediator mediator) {super(mediator);}public Purchase(){}// 采购IBM电脑public void butIBMComputer(int number){super.mediator.execute(OptionConstant.PURCHASE_BUY,number);}// 不再采购IBM电脑public void refuseBuyIBM(){System.out.println("不再采购IBM电脑");}
}
销售类(修改后)
/*** 销售类*/
public class Sale extends AbstractColleague {public Sale(AbstractMediator mediator) {super(mediator);}public Sale(){}// 销售IBM电脑public void sellIBMComputer(int number) {super.mediator.execute(OptionConstant.SALE_SELL, number);System.out.println("销售了" + number + "台IBM电脑");}// 反馈销售情况,0~100之间的变化,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(OptionConstant.SALE_OFF_SELL);}
}
库存类(修改后)
/*** 库存类*/
public class Stock extends AbstractColleague {private static int COMPUTER_NUMBER = 100;public Stock(AbstractMediator mediator) {super(mediator);}public Stock(){}// 库存增加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(OptionConstant.STOCK_CLEAR);}
}
入口类
public class MediatorMain {public static void main(String[] args) {notMediator();mediator();}// 中介者场景public static void mediator() {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 static void notMediator() {// 采购人员采购电脑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();}
}