设计模式学习笔记--Mediator 中介者模式.doc_第1页
设计模式学习笔记--Mediator 中介者模式.doc_第2页
设计模式学习笔记--Mediator 中介者模式.doc_第3页
设计模式学习笔记--Mediator 中介者模式.doc_第4页
设计模式学习笔记--Mediator 中介者模式.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

我们知道面向对象应用程序是由一组为了提供某种服务而彼此交互的对象组成。当彼此引用的对象数量比较少时,此时对象之间就为直接交互(点对点)。而当对象的数量增加时,这种直接交互会导致对象之间复杂的、混乱的引用,最后形成一张巨大的网,这就会影响应用程序的可维护性。同时,因为对象之间的高耦合,当一个对象直接引用其他的对象时,缩小了这些对象的复用范围。 因此:我们可使用一个“中介对象”来管理对象间的关联关系,避免相互交互的对象之间的紧耦合引用关系,从而更好地抵御变化。 以上的变化反映在下图: 中介者模式的相关角色如下图:由图可知,它的角色有:1、抽象同伴类角色 ColleagueBase。它是具体同伴类角色的基类,同时在它内部要引用到一个抽象中介类成员。2、抽象中介类角色 MediatorBase。它是具体中介类角色的基类. 它定义了要求其子类必须完成的方法,此方法可以被具体同伴类角色调用。3、具体同伴类角色 ConcreteColleague A/B.它们都继承自抽象同伴类角色 ColleagueBase,由具体同伴类角色所产生的实例化对象不再像以前那样相互之间直接联系,而是通过具体中介类角色进行统一协调和管理。在上图中所定义的Send和 Receive方法表示此类具有发送和接收信息的功能,但这时的发送和接收通讯是在ConcreteColleague 与Mediator之间进行,而不是在ConcreteColleague之间直接通讯,因为通过Mediator,它们与自己的同伴之间早就解除了耦合。4、具体中介类角色ConcreteMediator.它实现了抽象中介类角色所定义的通讯功能(此图中是Sendmessage功能)。但此功能是由具体同伴类角色实例ConcreteColleague来调用的,因为在具体中介类角色实例中保存了所有参与活动的具体同伴类角色实例的引用,它们在Mediator中介角色里集中放置与管理。当我们调用相关通讯功能时,通过中介者角色的管理,由它来决定信息通讯的目的地。下面我们用代码来加以说明我们有两段程序,一个用于演示中介者模式工作的基本流程,另一个我们使用中介者模式来模拟一个聊天室。一、基本流程示例代码1、抽象同伴类角色 AbsPeopleusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMyMediatorabstractclassAbsPeopleprotectedAbsMediatormediator;#region构造函数publicAbsPeople(AbsMediatormediator)this.mediator=mediator;#endregion2、抽象中介类角色 AbsMediatorusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMyMediator/抽象中介类角色AbsMediator/在这里定义了需要实现的通讯方法abstractclassAbsMediator/在Send方法中,传入的是AbsPeople接口,它是具体People类的基类publicabstractvoidSend(stringmessage,AbsPeoplepeople);3、具体同伴类角色 Student与TeacherusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMyMediator/定义具体同伴类角色Student类Teacher类4、具体中介类角色 RealMediatorusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMyMediatorclassRealMediator:AbsMediator/在Mediator的具体类中融合了所有相关联的PeopleprivateStudent_pstudent;privateTeacher_pteacher;publicStudentPStudentset_pstudent=value;publicTeacherPBset_pteacher=value;publicoverridevoidSend(stringmessage,AbsPeoplepeople)if(people=_pstudent)_pteacher.Notify(message);if(people=_pteacher)_pstudent.Notify(message);5、客户应用#region流程示范Console.WriteLine(-流程示例-);RealMediatorrm=newRealMediator();StudentpStudent=newStudent(rm);TeacherpTeacher=newTeacher(rm);rm.PStudent=pStudent;rm.PB=pTeacher;pStudent.Send(老师好);pTeacher.Send(同学们好.);Console.ReadKey();#endregion二、中介者模式实现的聊天室1、抽象同伴类角色 AbsParticipantusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMyMediator/定义抽象同伴类角色/此处定义了两个属性,一个是Name,一个是抽象中介类对象publicabstractclassAbsParticipantChatRoom属性,它是AbsChatRoom类对象Name属性构造函数定义其子类需要实现的功能2、抽象中介类角色 AbsChatRoomusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMyMediator/定义一个抽象类AbsChatRoom,它是RealChatRoom的基类/此处定义了两个需要实现的功能,一个是注册,一个是发送信息publicabstractclassAbsChatRoompublicabstractvoidRegister(AbsParticipantparticipant);publicabstractvoidSend(stringfrom,stringto,stringmessage);3、具体同伴类角色 Boy与GirlusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMyMediator/此处定义了两个抽象同伴类角色AbsParticipant的子类,一个是Boy,一个是Girl男士Partitcipant类女士Participant类4、具体中介类角色 RealChatRoomusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceMyMediator#region定义一个具体的ChatRoom,它继承自抽象类AbsChatRoom,并实现AbsChatRoom定义的功能classRealChatRoom:AbsChatRoom#region定义一个字典,用于管理参加聊天的人员privateDictionary_participants=newDictionary();#endregion#region实现注册功能publicoverridevoidRegister(AbsParticipantparticipant)/如果新加入一个人,他/她不在当前人员列表中,则把此新人加入到列表中if(!_participants.ContainsValue(participant)_participantsparticipant.Name=participant;participant.ChatRoom=this;#endregion#region实现发送信息功能publicoverridevoidSend(stringfrom,stringto,stringmessage)AbsParticipantpt=_participantsto;if(pt!=null)pt.Receive(from,message);/如果有此人,则调用他/她的Receive方法#endregion#endregion5、客户应用#region聊天室示例Console.WriteLine();Console.WriteLine();Console.WriteLine(-聊天室示例-);RealChatRoomroom=newRealChatRoom();AbsParticipantWangJun=newBoy(王军);AbsParticipantZhouQiang=newBoy(周强);AbsParticipantLiWeiDong=newBoy(李卫东);AbsParticipantYuanHui=newBoy(袁晖);AbsParticipantSunLing=newGirl(孙玲);AbsParticipantDenLiLi=newGirl(邓丽丽);room.Register(WangJun);room.Register(ZhouQiang);room.Register(LiWeiDong);room.Register(YuanHui);room.Register(SunLing);room.Register(DenLiLi);WangJun.Send(孙玲,你好孙玲);/此处实质是调用RealChatRoom定义的Send功能来完成Send操作ZhouQiang.Send(李卫东,哥们在吗?);LiWeiDong.Send(周强,我才上来,今天工作忙不忙?);Yua

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论