模式設(shè)計c#--行為型--mediator
| 名稱 | Mediator |
| 結(jié)構(gòu) |
|
| 意圖 | 用一個中介對象來封裝一系列的對象交互。中介者使各對象不需要顯式地相互引用,從而使其耦合松散,而且可以獨立地改變它們之間的交互。 |
| 適用性 |
|
namespace
?Mediator_DesignPattern
{
????
using
?System;
????
class
?Mediator?
????
{
????????
private
?DataProviderColleague?dataProvider;
????????
private
?DataConsumerColleague?dataConsumer;
????????
public
?
void
?IntroduceColleagues(DataProviderColleague?c1,?DataConsumerColleague?c2)
????????
{
????????????dataProvider?
=
?c1;
????????????dataConsumer?
=
?c2;????????????
????????}
????????
????????
public
?
void
?DataChanged()
????????
{
????????????
int
?i?
=
?dataProvider.MyData;
????????????dataConsumer.NewValue(i);
????????}
????}
????
class
?DataConsumerColleague?
????
{
????????
public
?
void
?NewValue(
int
?i)
????????
{
????????????Console.WriteLine(
"
New?value?{0}
"
,?i);
????????}
????}
????
class
?DataProviderColleague
????
{
????????
private
?Mediator?mediator;
????????
private
?
int
?iMyData
=
0
;
????????
public
?
int
?MyData?
????????
{
????????????
get
?
????????????
{
????????????????
return
?iMyData;
????????????}
????????????
set
?
????????????
{
????????????????iMyData?
=
?value;
????????????}
????????}
????????
public
?DataProviderColleague(Mediator?m)
????????
{
????????????mediator?
=
?m;
????????}
????????
public
?
void
?ChangeData()
????????
{
????????????iMyData?
=
?
403
;
????????????
//
?Inform?mediator?that?I?have?changed?the?data
????????????
if
?(mediator?
!=
?
null
)
????????????????mediator.DataChanged();????
????????}
????????
????}
????
/**/
///
?
<summary>
????
///
????Summary?description?for?Client.
????
///
?
</summary>
????
public
?
class
?Client
????
{
????????
public
?
static
?
int
?Main(
string
[]?args)
????????
{????????????
????????????Mediator?m?
=
?
new
?Mediator();
????????????DataProviderColleague?c1?
=
?
new
?DataProviderColleague(m);
????????????DataConsumerColleague?c2?
=
?
new
?DataConsumerColleague();
????????????m.IntroduceColleagues(c1,c2);
????????????c1.ChangeData();
????????????
return
?
0
;
????????}
????}
}

//?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);
????}
??}
}
?
posted on 2006-01-03 16:10 夢在天涯 閱讀(751) 評論(0) 編輯 收藏 引用 所屬分類: Design pattern

