设计模式学习笔记--中介者模式

Posted bzyzhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式学习笔记--中介者模式相关的知识,希望对你有一定的参考价值。

 1 using System;
 2 
 3 namespace Mediator
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/1 22:01:20 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Colleague说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public abstract class Colleague
12     {
13         protected Mediator mediator;
14 
15         public Colleague(Mediator mediator)
16         {
17             this.mediator = mediator;
18         }
19     }
20 }
View Code
 1 using System;
 2 
 3 namespace Mediator
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/1 22:03:37 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// ConcreteColleague1说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class ConcreteColleague1:Colleague
12     {
13         public ConcreteColleague1(Mediator mediator)
14             : base(mediator)
15         { }
16 
17         public void Send(string message)
18         {
19             mediator.Send(message,this);
20         }
21 
22         public void Notify(string message)
23         {
24             Console.WriteLine("同事1得到消息:"+message);
25         }
26     }
27 }
View Code
 1 using System;
 2 
 3 namespace Mediator
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/1 22:05:57 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// ConcreteColleague2说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class ConcreteColleague2 : Colleague
12     {
13         public ConcreteColleague2(Mediator mediator)
14             : base(mediator)
15         {
16         }
17 
18         public void Send(string message)
19         {
20             mediator.Send(message, this);
21         }
22 
23         public void Notify(string message)
24         {
25             Console.WriteLine("同事2得到消息:" + message);
26         }
27     }
28 }
View Code
 1 using System;
 2 
 3 namespace Mediator
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/1 22:00:29 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Mediator说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public abstract class Mediator
12     {
13         public abstract void Send(string message,Colleague colleague);
14     }
15 }
View Code
 1 using System;
 2 
 3 namespace Mediator
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/6/1 22:02:44 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// ConcreteMediator说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class ConcreteMediator:Mediator
12     {
13         private ConcreteColleague1 colleague1;
14         private ConcreteColleague2 colleague2;
15 
16         public ConcreteColleague1 Colleague1
17         {
18             set { colleague1 = value; }
19         }
20 
21         public ConcreteColleague2 Colleague2
22         {
23             set { colleague2 = value; }
24         }
25         public override void Send(string message, Colleague colleague)
26         {
27             if (colleague == colleague1)
28             {
29                 colleague2.Notify(message);
30             }
31             else
32             {
33                 colleague1.Notify(message);
34             }
35         }
36     }
37 }
View Code
 1 namespace Mediator
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             ConcreteMediator concreteMediator = new ConcreteMediator();
 8 
 9             ConcreteColleague1 colleague1 = new ConcreteColleague1(concreteMediator);
10             ConcreteColleague2 colleague2 = new ConcreteColleague2(concreteMediator);
11 
12             concreteMediator.Colleague1 = colleague1;
13             concreteMediator.Colleague2 = colleague2;
14 
15             colleague1.Send("吃过饭了吗?");
16             colleague2.Send("没有呢,你打算请客?");
17         }
18     }
19 }
View Code

 

以上是关于设计模式学习笔记--中介者模式的主要内容,如果未能解决你的问题,请参考以下文章

Java设计模式学习笔记

设计模式学习(19)- 中介者模式

中介者模式(Mediator) 笔记

《大话设计模式》笔记——行为型模式2

设计模式目录

MVC代码结构模式moogoDB的学习