<外观模式>
看外观模式,自己刚开始的简单理解就是这个模式特别省事,方便自己。
定义:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
讲述:外观模式是一种使用频率非常高的结构型设计模式,它通过引入一个外观角色来简化客户端与子系统之间的交互,为复杂的子系统调用提供一个统一接口,降低子系统与客户端的耦合度,且客户端调用非常方便。
说一个很常见的例子,比如去咖啡馆喝咖啡,享受一下生活。其实喝咖啡平常在家也是很常见的事情,不过这就不是单纯的享受生活了,而是学习累了,用来稍微的缓解一下疲劳而已。
代码实现:
- <strong><span style="font-family:SimHei;font-size:18px;">namespace 享受喝咖啡
- {
- class Program
- {
- static void Main(string[] args) //服务员直接就给充好咖啡,放到面前。这就是享受生活
- {
- Waiter wait = new Waiter();
- wait.fill();
- Console.Read();
- }
- }
- //拿铁咖啡
- class CafeLatte
- {
- public void Fill()
- {
- Console.WriteLine("冲拿铁咖啡");
- }
- }
- //牛奶咖啡
- class WhiteCoffee
- {
- public void fill()
- {
- Console.WriteLine("冲牛奶咖啡");
- }
- }
- class MochaCoffee
- {
- public void fill()
- {
- Console.WriteLine("冲摩卡咖啡");
- }
- }
- //Waiter
- class Waiter //增加一个服务者类
- {
- CafeLatte CL;
- WhiteCoffee WC;
- MochaCoffee MC;
- public Waiter ()
- {
- CL = new CafeLatte();
- WC = new WhiteCoffee();
- MC = new MochaCoffee();
- }
- public void fill()
- {
- CL.Fill();
- WC.fill();
- MC.fill();
- }</span></strong>
一个子系统的外部与其内部的通信通过一个统一的外观类进行,外观类将客户类与子系统的内部复杂性分隔开,使得客户类只需要与外观角色打交道,而不需要与子系统内部的很多对象打交道。
<中介者模式>
定义:用一个中介对象来封装一系列的对象交互。中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
解决的问题:中介者模式一般应用于一组对象以定义良好但是复杂的方法进行通信的场合,以及想定制一个分布在多个类中的行为,而不想生成太多的子类的场合。
结构图如下:
具体代码:
- <strong><span style="font-family: SimHei;"><span style="font-size: 18px; color: rgb(51, 51, 51);">namespace 感情交流
- {
- class Program
- {
- static void Main(string[] args)
- {
- QQ F = new QQ();
- ConcreteFriend1 c1 = new ConcreteFriend1(F); //两个好友同时了解QQ这个中介对象
- ConcreteFriend2 c2 = new ConcreteFriend2(F);
- F.Friend1 = c1; //让中介者QQ认识各个交流对象
- F.Friend2 = c2;
- c1.Send("晚饭吃过了吗?"); //两者的信息通过中介者转发
- c2.Send("没有呢,这么问,打算请客吗?");
- Console.Read();
- }
- }
- </span><span style="font-size: 18px; color: rgb(255, 0, 0);">//交流 抽象中介者类</span><span style="font-size: 18px; color: rgb(51, 51, 51);">
- abstract class Communication
- {
- //定义一个抽象的发送消息方法,得到交流者对象和发送消息
- public abstract void Send(string message,Friend friend);
- }
- </span><span style="font-size: 18px; color: rgb(255, 0, 0);">//Friend 抽象同事类</span><span style="font-size: 18px; color: rgb(51, 51, 51);">
- abstract class Friend
- {
- protected Communication communication;
- public Friend (Communication communication)
- {
- this.communication = communication; //构造方法,得到中介者对象
- }
- }
- </span><span style="font-size: 18px; color: rgb(255, 0, 0);">//QQ 具体中介者类</span><span style="font-size: 18px; color: rgb(51, 51, 51);">
- class QQ:Communication
- {
- private ConcreteFriend1 friend1;
- private ConcreteFriend2 friend2;
- public ConcreteFriend1 Friend1
- {
- set { friend1 = value; }
- }
- public ConcreteFriend2 Friend2
- {
- set { friend2 = value; }
- }
- public override void Send(string message,Friend friend)
- {
- if (friend ==friend1 ) //重新发送信息的方法,根据对象作出选择判断,通知对象
- {
- friend2.Notify(message);
- }
- else
- {
- friend1.Notify(message );
- }
- }
- }
- </span><span style="font-size: 18px; color: rgb(255, 0, 0);">//具体各种聊天人物</span><span style="color: rgb(51, 51, 51);"><span style="font-size:18px;">
- class ConcreteFriend1:Friend
- {
- public ConcreteFriend1 (Communication communication):base(communication )
- {
- }
- public void Send(string message)
- {
- communication.Send(message, this);
- }
- public void Notify(string message)
- {
- Console.WriteLine("小刘得到消息:" + message);
- }
- }
- class ConcreteFriend2:Friend
- {
- public ConcreteFriend2 (Communication communication):base (communication )
- {
- }
- public void Send(string message)
- {
- communication.Send(message, this);
- }
- public void Notify(string message)
- {
- Console.WriteLine("小李得到消息:" + message);
- }
- }</span><span style="font-size:32px;">
- </span></span></span></strong>
<比较>
外观模式 | 中介者模式 |
结构型模式 | 行为型模式 |
对子系统提供统一接口 | 用一个中介对象来封装一系列同事对象的交互行为 |
模式协议是单向 | 模式协议是双向 |
所有的请求处理都委托给子系统完成 | 由中心协调同事类和中心本身共同完成业务 |