博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
极速理解设计模式系列:15.中介者模式(Mediator Pattern)
阅读量:6999 次
发布时间:2019-06-27

本文共 2133 字,大约阅读时间需要 7 分钟。

五个角色:抽象中介者(Mediator)、具体中介者(ConcreteMediator)、同事类(Colleague)、具体同事类(ConcreteColleague)、客户端(Client) 

        抽象中介者(Mediator):定义接口让同事对象之间通讯

        具体中介者(ConcreteMediator):实现接口,并且协调维护同事对象

        同事类(Colleague):其内部引用中介者对象,并且提供一个接口发送消息

        具体同事类(ConcreteColleague):实现接口,向中介者发送消息

        客户端(Client) :让具体同事类认识中介者,并且让中介者认识同事,然后同事发送消息即可。

 实现思路:客户端让中介者和同事互相认识,然后同事向中介者发送消息,中介者将消息转发给另外的需要接收消息的同事。

 类图:

应用场景:一个用户使用中国移动的电话向另外一个用户发消息。

分析:用户发消息给另外一个用户的时候,实际上用户是发送消息给中国移动,然后中国移动信息控制器发送消息给另外一个用户,这里中介者是中国移动。

        下面我们在控制台程序去演示一下如何使用Mediator Pattern:

        一、抽象中介者(Mediator)

 

 
  1. //抽象中介者(Mediator) 
  2. abstract class ChinaMobile 
  3.     public abstract void Phone(string talkMessage, User user); 

        二、具体中介者(ConcreteMediator)

 

 
  1. //具体中介者(ConcreteMediator) 
  2. class ConcreteChinaMobile:ChinaMobile 
  3.     public User1 User1 { get; set; } 
  4.     public User2 User2 { get; set; } 
  5.     public override void Phone(string talkMessage, User user
  6.     { 
  7.         if (user == User1) 
  8.         { 
  9.             User2.GetMessage(talkMessage); 
  10.         } 
  11.         else 
  12.         { 
  13.             User1.GetMessage(talkMessage); 
  14.         } 
  15.     } 

        三、同事类(Colleague)

 

 
  1. //同事类(Colleague) 
  2. abstract class User 
  3.     protected ChinaMobile chinaMobile; 
  4.     public User(ChinaMobile mobile) 
  5.     { 
  6.         chinaMobile = mobile; 
  7.     } 

        四、具体同事类(ConcreteColleague)

 

 
  1. //具体同事类(ConcreteColleague) 
  2. class User1 : User 
  3.     public User1(ChinaMobile chinaMobile) : base(chinaMobile) { } 
  4.     public void Talk(string talkMessage) 
  5.     { 
  6.         chinaMobile.Phone(talkMessage, this); 
  7.     } 
  8.     public void GetMessage(string talkMessage) 
  9.     { 
  10.         Console.WriteLine("用户1得到消息:"+talkMessage); 
  11.     } 
  12.  
  13. //具体同事类(ConcreteColleague) 
  14. class User2 : User 
  15.     public User2(ChinaMobile chinaMobile) : base(chinaMobile) { } 
  16.     public void Talk(string talkMessage) 
  17.     { 
  18.         chinaMobile.Phone(talkMessage, this); 
  19.     } 
  20.     public void GetMessage(string talkMessage) 
  21.     { 
  22.         Console.WriteLine("用户2得到消息:" + talkMessage); 
  23.     } 

        五、客户端(Client) 

 

 
  1. //客户端(Client) 
  2. class Program 
  3.     static void Main(string[] args) 
  4.     { 
  5.  
  6.         ///让两个具体同事类认识中介者        
  7.         ConcreteChinaMobile cm = new ConcreteChinaMobile(); 
  8.  
  9.         ///让中介者认识具体对象          
  10.         User1 user1 = new User1(cm); 
  11.         User2 user2 = new User2(cm); 
  12.         cm.User1 = user1; 
  13.         cm.User2 = user2; 
  14.         Console.WriteLine("用户1给用户2发消息"); 
  15.         user1.Talk("你在哪儿?"); 
  16.         Console.WriteLine("用户2给用户1发消息"); 
  17.         user2.Talk("我在成都."); 
  18.  
  19.         Console.Read(); 
  20.     } 

        如需源码请点击  下载。

本文转自程兴亮 51CTO博客,原文链接:http://blog.51cto.com/chengxingliang/827019

转载地址:http://nflvl.baihongyu.com/

你可能感兴趣的文章
loadrunner中Run-time-Setting设置
查看>>
SSL连接建立过程分析(1)
查看>>
port与大全portClose方法
查看>>
美丽的数学家:如果您讨厌数学,这些其实都是人生故事
查看>>
Kettle 中转换(transformation)的执行过程
查看>>
读书笔记-互联网思维阅读10其中一本书《自由》
查看>>
Spark入门实战系列--5.Hive(上)--Hive介绍及部署
查看>>
tomcat设置web根目录
查看>>
CF 444B(DZY Loves FFT-时间复杂度)
查看>>
OCP-1Z0-051-名称解析-文章12称号
查看>>
UVALive 4225 Prime Bases 贪心
查看>>
Oracle B-tree、位图、全文索引三大索引性能比较及优缺点汇总
查看>>
[.net 面向对象程序设计进阶] (20) 反射(Reflection)(上)利用反射技术实现动态编程...
查看>>
【转】java中float与byte[]的互转 -- 不错
查看>>
[Ogre][地形][原创]基于OgreTerrain的地形实现
查看>>
shell登录模式及其相应配置文件(转)
查看>>
Puppet常识梳理
查看>>
web.config配置文件中的configSource属性
查看>>
发现一个国内牛逼的maven仓库,速度真的太快了
查看>>
Snmp配置
查看>>