詳解設計形式中的中介者形式在C++編程中的應用。本站提示廣大學習愛好者:(詳解設計形式中的中介者形式在C++編程中的應用)文章只能為提供參考,不一定能成為您想要的結果。以下是詳解設計形式中的中介者形式在C++編程中的應用正文
感化:用一個中介對象來封裝一系列的對象交互。中介者使各對象不須要顯式地互相援用,從而使其耦合松懈,並且可以自力地轉變它們之間的交互。
構造圖以下:
Colleage籠統同事類,而ConcreteColleage是詳細同時類,每一個詳細同事只曉得本身的行動,而不懂得其他同事類的情形,但它們卻都熟悉中介者對象,Mediator是籠統中介者,界說了同事對象到中介者對象的接口,ConcreteMediator是詳細中介者對象,完成籠統類的辦法,它須要曉得一切詳細同事類,並從詳細同事接收新聞,向詳細同事對象收回敕令。
Colleage類,籠統同事類
Mediator,籠統中介者類
解釋:
1. Mediator 形式中,每一個Colleague 保護一個 Mediator,當要停止通訊時,每一個詳細的 Colleague 直接向ConcreteMediator 發信息,至於信息發到哪裡,則由 ConcreteMediator 來決議。
2. ConcreteColleagueA 和 ConcreteColleagueB 不用保護對各自的援用,乃至它們也不曉得各個的存在。
3. 長處是,各個 Colleague 削減了耦合。
4. 缺陷是,因為 Mediator 掌握了集中化,因而就把 Colleague 之間的交互龐雜性變成了中介者的龐雜性,也就是中介者會變的比任何一個 Colleague 都龐雜。
中介者形式很輕易在體系中運用,也很輕易在體系中誤用。當體系中湧現了“多對多”交互龐雜的對象群時,不要急於應用中介者形式,而要先反思你的體系在設計上是否是公道。
Mediator的湧現削減了各個Colleage的耦合,使得可以自力地轉變和復用各個Colleage類和Mediator;
因為把對象若何協作停止了籠統,將中介作為一個自力的概念並將其封裝在一個對象中,如許存眷的對象就從對象各自自己的行動轉移到它們之間的交互下去,也就是站在一個更微觀的角度去對待體系。
因為ConcreteMediator掌握了集中化,因而就把交互龐雜性變成了中介者的龐雜性,這使得中介者會變得比任何一個ConcreteColleage都龐雜。
中介者形式的長處來自集中掌握,其缺陷也是它。
中介者形式普通運用於一組對象以界說優越然則龐雜的方法停止通訊的場所。
很好的例子:聊天室:
// 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);
}
}
}
實用場景: