中介者设计模式--练习
来源:互联网 发布:黑蚂蚁成年网络电视 编辑:程序博客网 时间:2024/05/15 03:15
1.UML
2.代码
Mediator
abstract class Mediator {public abstract void SendMessage(String message, Colleague colleague);}
Colleague
abstract class Colleague{protected Mediator mediator; //中介public Colleague(Mediator mediator){this.mediator = mediator;}}
ColleagueColleagueA
class ConcreteColleagueA extends Colleague{public ConcreteColleagueA(Mediator mediator){super(mediator);}public void Send(String message){mediator.SendMessage(message, this);}public void Notify(String message){System.out.println("同事A收到消息: "+message);}}ColleagueColleagueB
class ConcreteColleagueB extends Colleague{public ConcreteColleagueB(Mediator mediator){super(mediator);}public void Send(String message){mediator.SendMessage(message, this);}public void Notify(String message){System.out.println("同事B收到消息: "+message);}}
class ConcreteMediator extends Mediator{private ConcreteColleagueA A; //同事Aprivate ConcreteColleagueB B; //同事Bpublic void SendMessage(String message, Colleague colleague){if (colleague.equals(A)) //同事A发消息{B.Notify(message); //同事B接收消息}else{A.Notify(message);}} public void setConcreteColleagueA(ConcreteColleagueA A) { this.A = A; } public void setConcreteColleagueB(ConcreteColleagueB B) { this.B = B; }}Test
public class Test{public static void main(String[] args) {//中介ConcreteMediator m = new ConcreteMediator(); //同事ConcreteColleagueA A = new ConcreteColleagueA(m);ConcreteColleagueB B = new ConcreteColleagueB(m);//将同事添加到中介中,组网m.setConcreteColleagueA(A);m.setConcreteColleagueB(B);//同事间聊天A.Send("吃饭没");B.Send("还没吃,要不请我吃饭");}}
3.结果
0 0
- 中介者设计模式--练习
- 设计模式练习(15)——中介者模式
- 【设计模式】中介者
- 中介者设计模式
- 设计模式-中介者
- 设计模式-中介者
- [设计]中介者模式
- 设计模式 中介者模式
- 设计模式-中介者模式
- 设计模式 - 中介者模式
- 设计模式-中介者模式
- 设计模式:中介者模式
- 设计模式--中介者模式
- 设计模式-中介者模式
- 设计模式--中介者模式
- 设计模式-中介者模式
- 设计模式-中介者模式
- 设计模式-中介者模式
- AJAX file uploads in Rails using attachment_fu and responds_to_parent2
- DIV+CSS布局入门示例(目录
- [ACM] hdu 1051 Wooden Sticks
- 字符串匹配之后缀树
- 字符编码(一)
- 中介者设计模式--练习
- oracle 使用order by 对汉字进行多字段排序
- KMP字符串模式匹配详解
- Playing with ptrace, Part I
- 3dsmax蒙皮教程,加载bip动作(菜鸟程序版)
- 智能makefile
- vim 快速格式化代码
- 为什么plsql developer长时间执行sql会通信中断(3)
- hdu 1686 比较 hdu 2087 深入了解KMP