济南个人制作网站,php能用着手机网站开发,崇信县门户网站,深圳企业网站怎么做我们知道面向对象应用程序是由一组为了提供某种服务而彼此交互的对象组成。当彼此引用的对象数量比较少时#xff0c;此时对象之间就为直接交互#xff08;点对点)。而当对象的数量增加时#xff0c;这种直接交互会导致对象之间复杂的、混乱的引用,最后形成一张巨大的网此时对象之间就为直接交互点对点)。而当对象的数量增加时这种直接交互会导致对象之间复杂的、混乱的引用,最后形成一张巨大的网这就会影响应用程序的可维护性。同时因为对象之间的高耦合当一个对象直接引用其他的对象时缩小了这些对象的复用范围。 因此:我们可使用一个“中介对象”来管理对象间的关联关系避免相互交互的对象之间的紧耦合引用关系从而更好地抵御变化。 以上的变化反映在下图 中介者模式的相关角色如下图 由图可知它的角色有: 1、抽象同伴类角色 ColleagueBase。它是具体同伴类角色的基类同时在它内部要引用到一个抽象中介类成员。 2、抽象中介类角色 MediatorBase。它是具体中介类角色的基类. 它定义了要求其子类必须完成的方法此方法可以被具体同伴类角色调用。 3、具体同伴类角色 ConcreteColleague A/B. 它们都继承自抽象同伴类角色 ColleagueBase由具体同伴类角色所产生的实例化对象不再像以前那样相互之间直接联系而是通过具体中介类角色进行统一协调和管理。在上图中所定义的Send和 Receive方法表示此类具有发送和接收信息的功能但这时的发送和接收通讯是在ConcreteColleague 与Mediator之间进行而不是在ConcreteColleague之间直接通讯因为通过Mediator它们与自己的同伴之间早就解除了耦合。 4、具体中介类角色ConcreteMediator. 它实现了抽象中介类角色所定义的通讯功能(此图中是Sendmessage功能)。但此功能是由具体同伴类角色实例ConcreteColleague来调用的因为在具体中介类角色实例中保存了所有参与活动的具体同伴类角色实例的引用它们在Mediator中介角色里集中放置与管理。当我们调用相关通讯功能时通过中介者角色的管理由它来决定信息通讯的目的地。 下面我们用代码来加以说明 我们有两段程序一个用于演示中介者模式工作的基本流程另一个我们使用中介者模式来模拟一个聊天室。一、基本流程示例代码 1、抽象同伴类角色 AbsPeople Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyMediator { abstract class AbsPeople { protected AbsMediator mediator; #region 构造函数 public AbsPeople(AbsMediator mediator) { this.mediator mediator; } #endregion } } 2、抽象中介类角色 AbsMediator Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyMediator { //抽象中介类角色AbsMediator //在这里定义了需要实现的通讯方法 abstract class AbsMediator { //在Send方法中传入的是AbsPeople接口它是具体People类的基类 public abstract void Send(string message,AbsPeople people); } } 3、具体同伴类角色 Student与Teacher Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyMediator{ //定义具体同伴类角色 Student类#region Student类 class Student : AbsPeople { 构造函数#region 构造函数 public Student(AbsMediator mediator) : base(mediator) { } #endregion public void Send(string message) { mediator.Send(message,this); } public void Notify(string message) { Console.WriteLine(学生收到信息:{0},message); } } #endregion Teacher类#region Teacher类 class Teacher : AbsPeople { 构造函数#region 构造函数 public Teacher(AbsMediator mediator) : base(mediator) { } #endregion public void Send(string message) { mediator.Send(message, this); } public void Notify(string message) { Console.WriteLine(教师收到信息:{0}, message); } } #endregion} 4、具体中介类角色 RealMediator Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyMediator { class RealMediator: AbsMediator { //在Mediator的具体类中融合了所有相关联的People private Student _pstudent; private Teacher _pteacher; public Student PStudent { set { _pstudent value; } } public Teacher PB { set { _pteacher value; } } public override void Send(string message, AbsPeople people) { if(people_pstudent) { _pteacher.Notify(message); } if(people_pteacher) { _pstudent.Notify(message); } } } } 5、客户应用 Code #region 流程示范 Console.WriteLine(---------------流程示例-------------); RealMediator rm new RealMediator(); Student pStudent new Student(rm); Teacher pTeacher new Teacher(rm); rm.PStudent pStudent; rm.PB pTeacher; pStudent.Send(老师好); pTeacher.Send(同学们好.); Console.ReadKey(); #endregion 二、中介者模式实现的聊天室 1、抽象同伴类角色 AbsParticipant Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyMediator{ //定义抽象同伴类角色 //此处定义了两个属性一个是Name,一个是抽象中介类对象 public abstract class AbsParticipant { ChatRoom属性它是AbsChatRoom类对象#region ChatRoom属性它是AbsChatRoom类对象 private AbsChatRoom _chatroom; public AbsChatRoom ChatRoom { set { _chatroom value; } get { return _chatroom; } } #endregion Name属性#region Name属性 private string _name; public string Name { get { return _name; } } #endregion 构造函数#region 构造函数 public AbsParticipant(string name) { this._name name; } #endregion 定义其子类需要实现的功能#region 定义其子类需要实现的功能 //在此处功能实现时需要用到抽象中介类对象所具备的功能如下面的AbsParticipant类Send功能的实现就用到了AbsChatRoom类对象所具备的Send功能。 Send功能#region Send功能 public void Send(string to,string message) { _chatroom.Send(_name, to, message); //调用chatroom的Send功能来完成Participant的Send功能,此处的chatroom是一个具体的RealChatRoom } #endregion Receive功能#region Receive功能 public virtual void Receive(string from, string message) { Console.WriteLine({0}to{1}:{2},from,Name,message); } #endregion #endregion }} 2、抽象中介类角色 AbsChatRoom Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyMediator { //定义一个抽象类AbsChatRoom它是RealChatRoom的基类 //此处定义了两个需要实现的功能一个是注册一个是发送信息 public abstract class AbsChatRoom { public abstract void Register(AbsParticipant participant); public abstract void Send(string from, string to, string message); } } 3、具体同伴类角色 Boy与Girl Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyMediator{ //此处定义了两个抽象同伴类角色AbsParticipant的子类一个是Boy,一个是Girl 男士Partitcipant类#region 男士Partitcipant类 public class Boy:AbsParticipant { public Boy(string name) : base(name) { } public override void Receive(string from, string message) { Console.WriteLine(-------{0}听到:----------------, this.Name); base.Receive(from, message); } } #endregion 女士Participant类#region 女士Participant类 public class Girl : AbsParticipant { public Girl(string name) : base(name) { } public override void Receive(string from, string message) { Console.WriteLine(-------{0}听到:----------------,this.Name); base.Receive(from, message); } } #endregion} 4、具体中介类角色 RealChatRoom Codeusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyMediator { #region 定义一个具体的ChatRoom,它继承自抽象类AbsChatRoom并实现AbsChatRoom定义的功能 class RealChatRoom:AbsChatRoom { #region 定义一个字典用于管理参加聊天的人员 private Dictionarystring, AbsParticipant _participants new Dictionarystring, AbsParticipant(); #endregion #region 实现注册功能 public override void Register(AbsParticipant participant) { //如果新加入一个人他/她不在当前人员列表中则把此新人加入到列表中 if(!_participants.ContainsValue(participant)) { _participants[participant.Name] participant; } participant.ChatRoom this; } #endregion #region 实现发送信息功能 public override void Send(string from, string to, string message) { AbsParticipant pt _participants[to]; if(pt!null) { pt.Receive(from, message); //如果有此人则调用他/她的Receive方法 } } #endregion } #endregion } 5、客户应用 Code #region 聊天室示例 Console.WriteLine(); Console.WriteLine(); Console.WriteLine(---------------聊天室示例-------------); RealChatRoom room new RealChatRoom(); AbsParticipant WangJun new Boy(王军); AbsParticipant ZhouQiang new Boy(周强); AbsParticipant LiWeiDong new Boy(李卫东); AbsParticipant YuanHui new Boy(袁晖); AbsParticipant SunLing new Girl(孙玲); AbsParticipant DenLiLi new Girl(邓丽丽); 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(周强, 我才上来今天工作忙不忙); YuanHui.Send(孙玲, 昨天看到你哥了); SunLing.Send(邓丽丽, 作业做完没有); DenLiLi.Send(周强, 你是哪里的); Console.ReadKey(); #endregion 程序如下图: 运行效果如下: 适用性 1.一组对象以定义良好但是复杂的方式进行通信。产生的相互依赖关系结构混乱且难以理解。 2.一个对象引用其他很多对象并且直接与这些对象通信导致难以复用该对象。 3.想定制一个分布在多个类中的行为而又不想生成太多的子类。 前往设计模式学习笔记清单 转载于:https://www.cnblogs.com/wsdj-ITtech/archive/2009/10/09/1579857.html