中介者模式

编程技术  /  houtizong 发布于 3年前   114
<!--StartFragment-->

抽象中介者(Mediator) 角色: 抽象中介者角色定义统一的接口用于各同事角色之间的通信。

package MediatorPattern.mediator;import MediatorPattern.colleague.Purchase;import MediatorPattern.colleague.Sale;import MediatorPattern.colleague.Store;public abstract class AbstractMediator {protected Purchase purchase;protected Sale sale;protected Store store;public abstract void execute(String str,Object...args);public void setPurchaseDepartment(Purchase purchase) {this.purchase = purchase;}public void setSaleDepartment(Sale sale) {this.sale = sale;}public void setStoreDepartment(Store store) {this.store = store;}}

 

 

<!--StartFragment-->

具体中介者(Concrete Mediator) 角色: 具体中介者角色通过协调各同事角色实现协作行为, 因此它必须依赖于各个同事角色。

package MediatorPattern.mediator;public class Mediator extends AbstractMediator{public void execute(String str,Object...args){if ("clearStock".equals(str)) {this.clearStock();}else if ("sell".equals(str)) {int number = (Integer) args[0];this.sell(number);}else if ("buy".equals(str)) {int number = (Integer) args[0];this.buy(number);}else if ("offSale".endsWith(str)) {this.offSale();}}private void clearStock(){purchase.refushBuy();int number = store.getStore();sale.sell(number);}private void sell(int number){System.out.println("销售部门销售:"+number);store.decrease(number);System.out.println("库存还有:"+store.getStore());}private void offSale(){System.out.println("销售部门折价销售");}private void buy(int number){int saleStatu = sale.getSaleStatus();if (saleStatu>80) {System.out.println("销售业绩好,采购部门购买:"+number);store.increase(number);}else {System.out.println("销售业绩不好,采购部门只采购一半购买:"+number/2);store.increase(number/2);}System.out.println("库存还有:"+store.getStore());}}

 

 

<!--StartFragment-->

同事(Colleague) 角色: 每一个同事角色都知道中介者角色, 而且与其他的同事角色通信的时候, 一

定要通过中介者角色协作。 每个同事类的行为分为两种: 一种是同事本身的行为, 比如改变对象本身的状

态, 处理自己的行为等等, 这种方法叫做自发行为(Self-Method), 与其他的同事类或中介者没有任何的依

赖; 第二种是是必须依赖中介者才能完成的行为, 叫做依赖方法(Dep-Method)。

package MediatorPattern.colleague;import MediatorPattern.mediator.AbstractMediator;public abstract class AbstractColleague {protected AbstractMediator mediator;public abstract void setMediator(AbstractMediator mediator);}

 

package MediatorPattern.colleague;import MediatorPattern.mediator.AbstractMediator;public class Purchase extends AbstractColleague{public void buy(int number){mediator.execute("buy",number);}public void refushBuy(){System.out.println("采购部门拒绝购买");}@Overridepublic void setMediator(AbstractMediator mediator) {super.mediator = mediator;mediator.setPurchaseDepartment(this);}}

 

package MediatorPattern.colleague;import java.util.Random;import MediatorPattern.mediator.AbstractMediator;public class Sale extends AbstractColleague{@Overridepublic void setMediator(AbstractMediator mediator) {super.mediator = mediator;mediator.setSaleDepartment(this);}public void sell(int number){mediator.execute("sell",number);}public int getSaleStatus(){Random random = new Random();int num = random.nextInt(100);System.out.println("销售情况:"+num);return num;}public void offSale(){ mediator.execute("offSale");}}

 

package MediatorPattern.colleague;import MediatorPattern.mediator.AbstractMediator;public class Store extends AbstractColleague{private int number = 100;@Overridepublic void setMediator(AbstractMediator mediator) {super.mediator = mediator;mediator.setStoreDepartment(this);}public void increase(int num){System.out.println("库存增加:"+num);number = number + num;}public void decrease(int num){System.out.println("库存减少:"+num);number = number - num;}public void clearStock(){ System.out.println("进行清仓大处理");mediator.execute("clearStock");}public int getStore(){return number;}}

 

客户端调用:

package MediatorPattern;import MediatorPattern.colleague.Purchase;import MediatorPattern.colleague.Sale;import MediatorPattern.colleague.Store;import MediatorPattern.mediator.AbstractMediator;import MediatorPattern.mediator.Mediator;public class Client {/** * @param args */public static void main(String[] args) {AbstractMediator mediator = new Mediator();Purchase purchase = new Purchase();Store store = new Store();Sale sale = new Sale();purchase.setMediator(mediator);store.setMediator(mediator);sale.setMediator(mediator);purchase.buy(100);sale.sell(20);store.clearStock();}}

 <!--EndFragment--><!--EndFragment--><!--EndFragment-->

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客