首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

设计形式-中介者模式(demiator pattern)

2013-08-04 
设计模式----中介者模式(demiator pattern)AbstractMediator.java代码如下:package com.designPattern.med

设计模式----中介者模式(demiator pattern)
AbstractMediator.java代码如下:

package com.designPattern.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);}


Mediator.java代码如下:
package com.designPattern.mediator;public class Mediator extends AbstractMediator{@Overridepublic void execute(String str, Object... objects) {      if(str.equals("purchase")){      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(int 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(int 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();}}


AbstractColleague.java代码如下:
package com.designPattern.mediator;public abstract class AbstractColleague { protected AbstractMediator mediator ;  public AbstractColleague(AbstractMediator mediator){ this.mediator = mediator; }}


Purchase.java代码如下:
package com.designPattern.mediator;public class Purchase extends AbstractColleague{public Purchase(AbstractMediator mediator) {super(mediator);}public void buyIBMComputer(int number){super.mediator.execute("purchase", number);}public void refuseBuyIBM(){System.out.println("不再采购IBM电脑");}}


Sale.java代码如下:
package com.designPattern.mediator;import java.util.Random;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("销售IBM电脑"+number+"台");}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");}}


Stock.java代码如下:
package com.designPattern.mediator;public class Stock extends AbstractColleague{public Stock(AbstractMediator mediator) {super(mediator);}private static int COMPUTER_NUMBER = 100;public void increase(int number){COMPUTER_NUMBER = COMPUTER_NUMBER+number;System.out.println("库存数量为:"+COMPUTER_NUMBER);}public void decrease(int number){COMPUTER_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");}}


Client.java代码如下:
package com.designPattern.mediator;public class Client {public static void main(String[] args) {AbstractMediator mediator = new Mediator();System.out.println("---------采购人员采购电脑---------");Purchase purchase = new Purchase(mediator);purchase.buyIBMComputer(100);System.out.println("----------销售人员销售IBM电脑-----------");Sale sale = new Sale(mediator);sale.sellIBMComputer(1);System.out.println("-----------库房管理人员清库处理--------------");Stock stock = new Stock(mediator);stock.clearStock();}}


运行结果如下:
---------采购人员采购电脑---------IBM电脑的销售量为:27采购IBM电脑:50台----------销售人员销售IBM电脑-----------库存数量为:99销售IBM电脑1台-----------库房管理人员清库处理--------------清理存货数量为:99折价销售IBM电脑99台不再采购IBM电脑

热点排行