外观模式 VS 中介者模式

<外观模式>

看外观模式,自己刚开始的简单理解就是这个模式特别省事,方便自己。

定义:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

讲述:外观模式是一种使用频率非常高的结构型设计模式,它通过引入一个外观角色来简化客户端与子系统之间的交互,为复杂的子系统调用提供一个统一接口,降低子系统与客户端的耦合度,且客户端调用非常方便。

说一个很常见的例子,比如去咖啡馆喝咖啡,享受一下生活。其实喝咖啡平常在家也是很常见的事情,不过这就不是单纯的享受生活了,而是学习累了,用来稍微的缓解一下疲劳而已。


代码实现:

  1. <strong><span style="font-family:SimHei;font-size:18px;">namespace 享受喝咖啡  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)   //服务员直接就给充好咖啡,放到面前。这就是享受生活  
  6.         {  
  7.             Waiter wait = new Waiter();  
  8.             wait.fill();  
  9.             Console.Read();  
  10.         }  
  11.     }  
  12.     //拿铁咖啡  
  13.     class CafeLatte  
  14.     {  
  15.         public void Fill()  
  16.         {  
  17.             Console.WriteLine("冲拿铁咖啡");  
  18.         }  
  19.     }  
  20.     //牛奶咖啡  
  21.     class WhiteCoffee  
  22.     {  
  23.         public void fill()  
  24.         {  
  25.             Console.WriteLine("冲牛奶咖啡");  
  26.         }  
  27.     }  
  28.     class MochaCoffee  
  29.     {  
  30.         public void fill()  
  31.         {  
  32.             Console.WriteLine("冲摩卡咖啡");  
  33.         }  
  34.     }  
  35.     //Waiter  
  36.     class Waiter   //增加一个服务者类  
  37.     {  
  38.         CafeLatte CL;  
  39.         WhiteCoffee WC;  
  40.         MochaCoffee MC;  
  41.         public Waiter ()  
  42.         {  
  43.             CL = new CafeLatte();  
  44.             WC = new WhiteCoffee();  
  45.             MC = new MochaCoffee();  
  46.         }  
  47.         public void fill()  
  48.         {  
  49.             CL.Fill();  
  50.             WC.fill();  
  51.             MC.fill();  
  52.         }</span></strong>  

    一个子系统的外部与其内部的通信通过一个统一的外观类进行,外观类将客户类与子系统的内部复杂性分隔开,使得客户类只需要与外观角色打交道,而不需要与子系统内部的很多对象打交道。

<中介者模式>

定义:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

解决的问题:中介者模式一般应用于一组对象以定义良好但是复杂的方法进行通信的场合,以及想定制一个分布在多个类中的行为,而不想生成太多的子类的场合。

 结构图如下:



具体代码:

  1. <strong><span style="font-family: SimHei;"><span style="font-size: 18px; color: rgb(51, 51, 51);">namespace 感情交流  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             QQ F = new QQ();  
  8.   
  9.             ConcreteFriend1 c1 = new ConcreteFriend1(F);   //两个好友同时了解QQ这个中介对象  
  10.             ConcreteFriend2 c2 = new ConcreteFriend2(F);  
  11.   
  12.             F.Friend1 = c1;    //让中介者QQ认识各个交流对象  
  13.             F.Friend2 = c2;  
  14.   
  15.             c1.Send("晚饭吃过了吗?");      //两者的信息通过中介者转发  
  16.             c2.Send("没有呢,这么问,打算请客吗?");  
  17.   
  18.             Console.Read();  
  19.         }  
  20.     }  
  21.     </span><span style="font-size: 18px; color: rgb(255, 0, 0);">//交流  抽象中介者类</span><span style="font-size: 18px; color: rgb(51, 51, 51);">  
  22.     abstract class Communication  
  23.     {  
  24.         //定义一个抽象的发送消息方法,得到交流者对象和发送消息  
  25.         public abstract void Send(string message,Friend friend);  
  26.     }  
  27.     </span><span style="font-size: 18px; color: rgb(255, 0, 0);">//Friend   抽象同事类</span><span style="font-size: 18px; color: rgb(51, 51, 51);">  
  28.     abstract class Friend  
  29.     {  
  30.         protected Communication communication;  
  31.   
  32.         public Friend (Communication communication)  
  33.         {  
  34.             this.communication = communication;  //构造方法,得到中介者对象  
  35.         }  
  36.     }  
  37.     </span><span style="font-size: 18px; color: rgb(255, 0, 0);">//QQ 具体中介者类</span><span style="font-size: 18px; color: rgb(51, 51, 51);">  
  38.     class QQ:Communication  
  39.     {  
  40.         private ConcreteFriend1 friend1;  
  41.         private ConcreteFriend2 friend2;  
  42.   
  43.         public ConcreteFriend1 Friend1  
  44.         {  
  45.             set { friend1 = value; }  
  46.         }  
  47.         public ConcreteFriend2 Friend2  
  48.         {  
  49.             set { friend2 = value; }  
  50.         }  
  51.         public override void Send(string message,Friend friend)  
  52.         {  
  53.             if (friend ==friend1 )   //重新发送信息的方法,根据对象作出选择判断,通知对象  
  54.             {  
  55.                 friend2.Notify(message);  
  56.             }  
  57.             else   
  58.             {  
  59.                 friend1.Notify(message );  
  60.             }  
  61.         }  
  62.     }  
  63.     </span><span style="font-size: 18px; color: rgb(255, 0, 0);">//具体各种聊天人物</span><span style="color: rgb(51, 51, 51);"><span style="font-size:18px;">  
  64.     class ConcreteFriend1:Friend  
  65.     {  
  66.         public   ConcreteFriend1 (Communication communication):base(communication )  
  67.         {  
  68.               
  69.         }  
  70.         public void Send(string message)  
  71.         {  
  72.             communication.Send(message, this);  
  73.         }  
  74.         public void Notify(string message)  
  75.         {  
  76.             Console.WriteLine("小刘得到消息:" + message);  
  77.         }  
  78.     }  
  79.     class ConcreteFriend2:Friend   
  80.     {  
  81.         public ConcreteFriend2 (Communication communication):base (communication )  
  82.         {  
  83.   
  84.         }  
  85.         public void Send(string message)  
  86.         {  
  87.             communication.Send(message, this);  
  88.         }  
  89.         public void Notify(string message)  
  90.         {  
  91.             Console.WriteLine("小李得到消息:" + message);  
  92.         }  
  93.     }</span><span style="font-size:32px;">  
  94. </span></span></span></strong>  

<比较>

外观模式中介者模式
结构型模式行为型模式
对子系统提供统一接口用一个中介对象来封装一系列同事对象的交互行为
模式协议是单向模式协议是双向
所有的请求处理都委托给子系统完成由中心协调同事类和中心本身共同完成业务
用一句话总结: 中介者模式适用于当事者双方不方便或者无能力联系的场合;门面模式以统一的接口对外提供服务,便于服务的使用为目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值