设计模式学习笔记--Mediator 中介者模式
?? ?我們知道面向對象應用程序是由一組為了提供某種服務而彼此交互的對象組成。當彼此引用的對象數量比較少時,此時對象之間就為直接交互(點對點)。而當對象的數量增加時,這種直接交互會導致對象之間復雜的、混亂的引用,最后形成一張巨大的網,這就會影響應用程序的可維護性。同時,因為對象之間的高耦合,當一個對象直接引用其他的對象時,縮小了這些對象的復用范圍。
??? 因此:我們可使用一個“中介對象”來管理對象間的關聯關系,避免相互交互的對象之間的緊耦合引用關系,從而更好地抵御變化。
?? 以上的變化反映在下圖:
???
?? 中介者模式的相關角色如下圖:
? 由圖可知,它的角色有:
?1、抽象同伴類角色 ColleagueBase。它是"具體同伴類角色"的基類,同時在它內部要引用到一個抽象中介類成員。
?2、抽象中介類角色 MediatorBase。它是"具體中介類角色"的基類. 它定義了要求其子類必須完成的方法,此方法可以被具體同伴類角色調用。
?3、具體同伴類角色 ConcreteColleague A/B. 它們都繼承自"抽象同伴類角色 ColleagueBase",由具體同伴類角色所產生的實例化對象不再像以前那樣相互之間直接聯系,而是通過"具體中介類角色"進行統一協調和管理。在上圖中所定義的Send和 Receive方法表示此類具有發送和接收信息的功能,但這時的發送和接收通訊是在ConcreteColleague 與Mediator之間進行,而不是在ConcreteColleague之間直接通訊,因為通過Mediator,它們與自己的同伴之間早就解除了耦合。
?4、具體中介類角色ConcreteMediator. 它實現了抽象中介類角色所定義的通訊功能(此圖中是Sendmessage功能)。但此功能是由具體同伴類角色實例ConcreteColleague來調用的,因為在具體中介類角色實例中保存了所有參與活動的具體同伴類角色實例的引用,它們在Mediator中介角色里集中放置與管理。當我們調用相關通訊功能時,通過中介者角色的管理,由它來決定信息通訊的目的地。
下面我們用代碼來加以說明
我們有兩段程序,一個用于演示中介者模式工作的基本流程,另一個我們使用中介者模式來模擬一個聊天室。
一、基本流程示例代碼
1、抽象同伴類角色 AbsPeople
using?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
using?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? Code
using?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?Dictionary<string,?AbsParticipant>?_participants?=?new?Dictionary<string,?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
總結
以上是生活随笔為你收集整理的设计模式学习笔记--Mediator 中介者模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 丰田汉兰达、国产宝马X5爬坡大比拼:还没
- 下一篇: LR重新安装的问题