极速理解设计模式系列:15.中介者模式(Mediator Pattern)
2011-10-17 10:48
381 查看
五个角色:抽象中介者(Mediator)、具体中介者(ConcreteMediator)、同事类(Colleague)、具体同事类(ConcreteColleague)、客户端(Client)
抽象中介者(Mediator):定义接口让同事对象之间通讯
具体中介者(ConcreteMediator):实现接口,并且协调维护同事对象
同事类(Colleague):其内部引用中介者对象,并且提供一个接口发送消息
具体同事类(ConcreteColleague):实现接口,向中介者发送消息
客户端(Client) :让具体同事类认识中介者,并且让中介者认识同事,然后同事发送消息即可。
实现思路:客户端让中介者和同事互相认识,然后同事向中介者发送消息,中介者将消息转发给另外的需要接收消息的同事。
[b] 类图:[/b]
应用场景:一个用户使用中国移动的电话向另外一个用户发消息。
分析:用户发消息给另外一个用户的时候,实际上用户是发送消息给中国移动,然后中国移动信息控制器发送消息给另外一个用户,这里中介者是中国移动。
下面我们在控制台程序去演示一下如何使用Mediator Pattern:
一、抽象中介者(Mediator)
二、具体中介者(ConcreteMediator)
三、同事类(Colleague)
四、具体同事类(ConcreteColleague)
五、客户端(Client)
如需源码请点击 MediatorPattern.rar 下载。
抽象中介者(Mediator):定义接口让同事对象之间通讯
具体中介者(ConcreteMediator):实现接口,并且协调维护同事对象
同事类(Colleague):其内部引用中介者对象,并且提供一个接口发送消息
具体同事类(ConcreteColleague):实现接口,向中介者发送消息
客户端(Client) :让具体同事类认识中介者,并且让中介者认识同事,然后同事发送消息即可。
实现思路:客户端让中介者和同事互相认识,然后同事向中介者发送消息,中介者将消息转发给另外的需要接收消息的同事。
[b] 类图:[/b]
应用场景:一个用户使用中国移动的电话向另外一个用户发消息。
分析:用户发消息给另外一个用户的时候,实际上用户是发送消息给中国移动,然后中国移动信息控制器发送消息给另外一个用户,这里中介者是中国移动。
下面我们在控制台程序去演示一下如何使用Mediator Pattern:
一、抽象中介者(Mediator)
//抽象中介者(Mediator) abstract class ChinaMobile { public abstract void Phone(string talkMessage, User user); }
二、具体中介者(ConcreteMediator)
//具体中介者(ConcreteMediator) class ConcreteChinaMobile:ChinaMobile { public User1 User1 { get; set; } public User2 User2 { get; set; } public override void Phone(string talkMessage, User user) { if (user == User1) { User2.GetMessage(talkMessage); } else { User1.GetMessage(talkMessage); } } }
三、同事类(Colleague)
//同事类(Colleague) abstract class User { protected ChinaMobile chinaMobile; public User(ChinaMobile mobile) { chinaMobile = mobile; } }
四、具体同事类(ConcreteColleague)
//具体同事类(ConcreteColleague) class User1 : User { public User1(ChinaMobile chinaMobile) : base(chinaMobile) { } public void Talk(string talkMessage) { chinaMobile.Phone(talkMessage, this); } public void GetMessage(string talkMessage) { Console.WriteLine("用户1得到消息:"+talkMessage); } } //具体同事类(ConcreteColleague) class User2 : User { public User2(ChinaMobile chinaMobile) : base(chinaMobile) { } public void Talk(string talkMessage) { chinaMobile.Phone(talkMessage, this); } public void GetMessage(string talkMessage) { Console.WriteLine("用户2得到消息:" + talkMessage); } }
五、客户端(Client)
//客户端(Client) class Program { static void Main(string[] args) { ///让两个具体同事类认识中介者 ConcreteChinaMobile cm = new ConcreteChinaMobile(); ///让中介者认识具体对象 User1 user1 = new User1(cm); User2 user2 = new User2(cm); cm.User1 = user1; cm.User2 = user2; Console.WriteLine("用户1给用户2发消息"); user1.Talk("你在哪儿?"); Console.WriteLine("用户2给用户1发消息"); user2.Talk("我在成都."); Console.Read(); } }
如需源码请点击 MediatorPattern.rar 下载。
相关文章推荐
- 极速理解设计模式系列:15.中介者模式(Mediator Pattern)
- 极速理解设计模式系列:23.装饰器模式(Decorator Pattern)
- 我所理解的设计模式(C++实现)——中介者模式(Mediator Pattern)
- 极速理解设计模式系列:16.迭代器模式(Iterator Pattern)
- 极速理解设计模式系列:17.命令模式(Command Pattern)
- 极速理解设计模式系列:21.简单工厂模式(Simple Factory Pattern)
- 我所理解的设计模式(C++实现)——中介者模式(Mediator Pattern)
- 极速理解设计模式系列:6.适配器模式(Adapter Pattern)
- 我所理解的设计模式(C++实现)——中介者模式(Mediator Pattern)
- 极速理解设计模式系列:1.建造者模式(Builder Pattern)
- 极速理解设计模式系列:6.适配器模式(Adapter Pattern)
- 极速理解设计模式系列:3.简单工厂模式(Simple Factory Pattern)
- 极速理解设计模式系列:1.建造者模式(Builder Pattern)
- 极速理解设计模式系列:10.抽象工厂模式(Abstract Factory Pattern)
- 极速理解设计模式系列:10.抽象工厂模式(Abstract Factory Pattern)
- 极速理解设计模式系列:4.抽象工厂模式(Abstract Factory Pattern)
- 极速理解设计模式系列:13.外观模式(Facade Pattern)
- 极速理解设计模式系列:13.外观模式(Facade Pattern)
- 极速理解设计模式系列:7.桥接模式(Bridge Pattern)
- 极速理解设计模式系列:7.桥接模式(Bridge Pattern)