詳解設(shè)計(jì)模式中的中介者模式在C++編程中的運(yùn)用
作用:用一個(gè)中介對(duì)象來(lái)封裝一系列的對(duì)象交互。中介者使各對(duì)象不需要顯式地相互引用,從而使其耦合松散,而且可以獨(dú)立地改變它們之間的交互。
結(jié)構(gòu)圖如下:
Colleage抽象同事類,而ConcreteColleage是具體同時(shí)類,每個(gè)具體同事只知道自己的行為,而不了解其他同事類的情況,但它們卻都認(rèn)識(shí)中介者對(duì)象,Mediator是抽象中介者,定義了同事對(duì)象到中介者對(duì)象的接口,ConcreteMediator是具體中介者對(duì)象,實(shí)現(xiàn)抽象類的方法,它需要知道所有具體同事類,并從具體同事接受消息,向具體同事對(duì)象發(fā)出命令。
Colleage類,抽象同事類
Mediator,抽象中介者類
說(shuō)明:
1. Mediator 模式中,每個(gè)Colleague 維護(hù)一個(gè) Mediator,當(dāng)要進(jìn)行通信時(shí),每個(gè)具體的 Colleague 直接向ConcreteMediator 發(fā)信息,至于信息發(fā)到哪里,則由 ConcreteMediator 來(lái)決定。
2. ConcreteColleagueA 和 ConcreteColleagueB 不必維護(hù)對(duì)各自的引用,甚至它們也不知道各個(gè)的存在。
3. 優(yōu)點(diǎn)是,各個(gè) Colleague 減少了耦合。
4. 缺點(diǎn)是,由于 Mediator 控制了集中化,于是就把 Colleague 之間的交互復(fù)雜性變?yōu)榱酥薪檎叩膹?fù)雜性,也就是中介者會(huì)變的比任何一個(gè) Colleague 都復(fù)雜。
中介者模式很容易在系統(tǒng)中應(yīng)用,也很容易在系統(tǒng)中誤用。當(dāng)系統(tǒng)中出現(xiàn)了“多對(duì)多”交互復(fù)雜的對(duì)象群時(shí),不要急于使用中介者模式,而要先反思你的系統(tǒng)在設(shè)計(jì)上是不是合理。
Mediator的出現(xiàn)減少了各個(gè)Colleage的耦合,使得可以獨(dú)立地改變和復(fù)用各個(gè)Colleage類和Mediator;
由于把對(duì)象如何協(xié)作進(jìn)行了抽象,將中介作為一個(gè)獨(dú)立的概念并將其封裝在一個(gè)對(duì)象中,這樣關(guān)注的對(duì)象就從對(duì)象各自本身的行為轉(zhuǎn)移到它們之間的交互上來(lái),也就是站在一個(gè)更宏觀的角度去看待系統(tǒng)。
由于ConcreteMediator控制了集中化,于是就把交互復(fù)雜性變?yōu)榱酥薪檎叩膹?fù)雜性,這使得中介者會(huì)變得比任何一個(gè)ConcreteColleage都復(fù)雜。
中介者模式的優(yōu)點(diǎn)來(lái)自集中控制,其缺點(diǎn)也是它。
中介者模式一般應(yīng)用于一組對(duì)象以定義良好但是復(fù)雜的方式進(jìn)行通信的場(chǎng)合。
很好的例子:聊天室:
// Mediator pattern -- Real World example
using System;
using System.Collections;
namespace DoFactory.GangOfFour.Mediator.RealWorld
{
// MainApp test application
class MainApp
{
static void Main()
{
// Create chatroom
Chatroom chatroom = new Chatroom();
// Create participants and register them
Participant George = new Beatle("George");
Participant Paul = new Beatle("Paul");
Participant Ringo = new Beatle("Ringo");
Participant John = new Beatle("John") ;
Participant Yoko = new NonBeatle("Yoko");
chatroom.Register(George);
chatroom.Register(Paul);
chatroom.Register(Ringo);
chatroom.Register(John);
chatroom.Register(Yoko);
// Chatting participants
Yoko.Send ("John", "Hi John!");
Paul.Send ("Ringo", "All you need is love");
Ringo.Send("George", "My sweet Lord");
Paul.Send ("John", "Can't buy me love");
John.Send ("Yoko", "My sweet love") ;
// Wait for user
Console.Read();
}
}
// "Mediator"
abstract class AbstractChatroom
{
public abstract void Register(Participant participant);
public abstract void Send(
string from, string to, string message);
}
// "ConcreteMediator"
class Chatroom : AbstractChatroom
{
private Hashtable participants = new Hashtable();
public override void Register(Participant participant)
{
if (participants[participant.Name] == null)
{
participants[participant.Name] = participant;
}
participant.Chatroom = this;
}
public override void Send(
string from, string to, string message)
{
Participant pto = (Participant)participants[to];
if (pto != null)
{
pto.Receive(from, message);
}
}
}
// "AbstractColleague"
class Participant
{
private Chatroom chatroom;
private string name;
// Constructor
public Participant(string name)
{
this.name = name;
}
// Properties
public string Name
{
get{ return name; }
}
public Chatroom Chatroom
{
set{ chatroom = value; }
get{ return chatroom; }
}
public void Send(string to, string message)
{
chatroom.Send(name, to, message);
}
public virtual void Receive(
string from, string message)
{
Console.WriteLine("{0} to {1}: '{2}'",
from, Name, message);
}
}
//" ConcreteColleague1"
class Beatle : Participant
{
// Constructor
public Beatle(string name) : base(name)
{
}
public override void Receive(string from, string message)
{
Console.Write("To a Beatle: ");
base.Receive(from, message);
}
}
//" ConcreteColleague2"
class NonBeatle : Participant
{
// Constructor
public NonBeatle(string name) : base(name)
{
}
public override void Receive(string from, string message)
{
Console.Write("To a non-Beatle: ");
base.Receive(from, message);
}
}
}
適用場(chǎng)景:
- 一組對(duì)象以定義良好但是復(fù)雜的方式進(jìn)行通信。產(chǎn)生的相互依賴關(guān)系結(jié)構(gòu)混亂且難以理解。
- 一個(gè)對(duì)象引用其他很多對(duì)象并且直接與這些對(duì)象通信,導(dǎo)致難以復(fù)用該對(duì)象。
- 想定制一個(gè)分布在多個(gè)類中的行為,而又不想生成太多的子類。
上一篇:在C++程序中開(kāi)啟和禁用Windows設(shè)備的無(wú)線網(wǎng)卡的方法
欄 目:C語(yǔ)言
下一篇:使用C語(yǔ)言編寫(xiě)基于TCP協(xié)議的Socket通訊程序?qū)嵗窒?/a>
本文標(biāo)題:詳解設(shè)計(jì)模式中的中介者模式在C++編程中的運(yùn)用
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2409.html
您可能感興趣的文章
- 01-10求子數(shù)組最大和的解決方法詳解
- 01-10深入二叉樹(shù)兩個(gè)結(jié)點(diǎn)的最低共同父結(jié)點(diǎn)的詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)- 解析最少換車次數(shù)的問(wèn)題詳解
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10HDOJ 1443 約瑟夫環(huán)的最新應(yīng)用分析詳解
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10如何查看進(jìn)程實(shí)際的內(nèi)存占用情況詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10APUE筆記之:進(jìn)程環(huán)境詳解
- 01-10深入第K大數(shù)問(wèn)題以及算法概要的詳解


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改


