设计模式——仲裁模式
仲裁者模式_只有一个老大(系统内的所有人都要上报行为并接受老大分配的任务)
/*** 仲裁者父类* @author maikec* @date 2019/5/14*/
public abstract class AbstractMediator {protected final Map<Integer, AbstractColleague> colleagueMap;public AbstractMediator(){colleagueMap = Collections.synchronizedMap( new HashMap() );}/*** 注入成员* @param id 成员id* @param colleague 成员*/public final void register(Integer id,AbstractColleague colleague){colleagueMap.put(id,colleague);}/*** 下发任务* @param id 成员id* @param msg 消息*/public abstract void operation(Integer id, String msg);
}/*** 成员父类* @author maikec* @date 2019/5/14*/
public abstract class AbstractColleague {protected AbstractMediator mediator;public AbstractColleague(){}public AbstractColleague(AbstractMediator mediator){this.mediator = mediator;}/*** 请求上报给仲裁者* @param toId 消息接收者id* @param msg*/public abstract void sendMsg(Integer toId, String msg);public void receiveMsg(String msg){System.out.println( this+" receive "+msg );}
}/*** @author maikec* @date 2019/5/14*/
public class ColleagueMediator extends AbstractMediator {@Overridepublic void operation(Integer id, String msg) {if (colleagueMap.isEmpty()){throw new IllegalStateException("需要注入成员");}if (colleagueMap.containsKey( id )){colleagueMap.get( id ).receiveMsg( msg );}else{throw new IllegalStateException( "未包含该成员:"+id );}}
}/*** @author maikec* @date 2019/5/14*/
public class ColleagueA extends AbstractColleague {public ColleagueA(){super();}public ColleagueA(AbstractMediator mediator){super(mediator);}@Overridepublic void sendMsg(Integer toId, String msg) {mediator.operation( toId, msg );}
}/*** @author maikec* @date 2019/5/14*/
public class ColleagueB extends AbstractColleague {public ColleagueB(){super();}public ColleagueB(AbstractMediator mediator){super(mediator);}@Overridepublic void sendMsg(Integer toId, String msg) {mediator.operation( toId, msg );}
}/*** @author maikec* @date 2019/5/14*/
public class MediatorDemo {public static void main(String[] args) {//创建仲裁者AbstractMediator colleagueMediator = new ColleagueMediator();//创建成员并设置仲裁者AbstractColleague colleagueA = new ColleagueA( colleagueMediator );AbstractColleague colleagueB = new ColleagueB( colleagueMediator );//注入成员colleagueMediator.register( 1,colleagueA );colleagueMediator.register( 2,colleagueB );colleagueA.sendMsg( 2,"Hi,I am A" );colleagueB.sendMsg( 1,"Hi,I am B" );}
}
复制代码
附录
github.com/maikec/patt… 个人GitHub设计模式案例
声明
引用该文档请注明出处
转载于:https://juejin.im/post/5ce8e76ce51d45108c59a4ae
设计模式——仲裁模式相关推荐
- Python设计模式-建造者模式
Python设计模式-建造者模式 代码基于3.5.2,代码如下; #coding:utf-8 #建造者模式 class Burger():name = ""price = 0.0d ...
- Python设计模式-状态模式
Python设计模式-状态模式 代码基于3.5.2,代码如下; #coding:utf-8 #状态模式class state():def writeProgram(self,work):raise N ...
- Python设计模式-备忘录模式
Python设计模式-备忘录模式 代码基于3.5.2,代码如下; #coding:utf-8 #备忘录模式 import randomclass gameCharacter():vitality = ...
- Python设计模式-解释器模式
Python设计模式-解释器模式 代码基于3.5.2,代码如下; #coding:utf-8 #解释器模式class PlayContext():play_text = Noneclass Expre ...
- Python设计模式-命令模式
Python设计模式-命令模式 代码基于3.5.2,代码如下; #coding:utf-8 #命令模式class barbecuer():def bakeButton(self):print(&quo ...
- Python设计模式-策略模式
Python设计模式-策略模式 代码基于3.5.2,代码如下; #coding:utf-8 #策略模式class sendInterface():def send(self,value):raise ...
- Python设计模式-外观模式
Python设计模式-外观模式 代码基于3.5.2,代码如下; #coding:utf-8 # 外观模式class AlarmSensor:def run(self):print("Alar ...
- Python设计模式-桥接模式
Python设计模式-桥接模式 基于Python3.5.2,代码如下 #coding:utf-8class Shape():name = ""param = "" ...
- Python设计模式-代理模式
Python设计模式-代理模式 基于Python3.5.2,代码如下 #coding:utf-8info_struct = dict() info_struct["addr"] = ...
- 说说设计模式~建造者模式(Builder)
建造者模式是我的"设计模式"里创建型模式里的最后一篇,这种模式在实现中,很多架构都用到了,如MVC,MVP,MVVM,它们都是有建造者模式的精髓的,即,创建与表现分享,我们的MVC ...
最新文章
- 取代Python?Rust凭什么
- “自拍神器”贴心实用功能大曝光
- 实战Cacti网络监控(1)——基础安装配置
- MyBatis 流式查询
- Docker入门-docker compose的使用
- SpringBoot操作MongoDB实现增删改查
- 飞鸽传书发现这个讲稿对现场
- php多表条件排除然后输出,php – 从具有多个要求的两个表中选择值,但仅使用其中一个...
- 找出不是两个数组共有的元素(学习去重复算法)
- 一步一步教你安装Nginx+PHP+Mysql
- 物化视图(materialized view) 实现数据迁移、数据定时同步
- python生产和消费模型_【Python】python 生产/消费模型
- 修改 jenkins 插件下载地址
- 先手获胜逻辑题_宣布障碍塔挑战赛的获胜者和开源版本
- 【学习笔记】【MAC】sdk自带uiautomatorviewer启动报错问题解决
- mysql 死锁分析_【mysql】MySQL知识整理-死锁分析-性能优化等
- springboot打jar包部署在linux(阿里云)服务器上项目启动成功但页面访问时提示无法访问此网站
- 洛谷P1567 统计天数
- SQLServer之创建唯一非聚集索引
- mysql使用什么语句为指定_在MySQL中,可以使用_____语句来为指定数据库添加用户。...