标签:ring err pen exe mil etc int soft str
依据这个设计出例如以下
类图
分析类图:三个子模块继承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 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(); } }
我是菜鸟,我在路上。
标签:ring err pen exe mil etc int soft str
原文地址:http://www.cnblogs.com/wzzkaifa/p/6932737.html