23种设计模式之我见----结构型设计模式之2(3)
来源:互联网 发布:中国m2历年数据 编辑:程序博客网 时间:2024/04/28 22:33
9.外观模式(Facade)
门面模式要求一个子系统的外部与其内部的通信必须通过一个统一的门面(Facade)对象进行,也就是对于子系统的操作通过暴漏出来的门面进行操作就行,并不需要了解具体的操作是什么,比如对于玩具汽车的操作,我们只需要按下前进按钮就行了,并不需要知道其具体是如何操作的。
这是对外观模式,也就是门面模式使用最多的一张图,在这个图里面,有两个角色:
门面(Facade)角色:客户端可以调用这个角色的方法。此角色知晓相关的(一个或者多个)子系统的功能和责任。在正常情况下,本角色会将所有从客户端发来的请求委派到相应的子系统去。
子系统(subsystem)角色:可以同时有一个或者多个子系统。每一个子系统都不是一个单独的类,而是一个类的集合。每一个子系统都可以被客户端直接调用,或者被门面角色调用。子系统并不知道门面的存在,对于子系统而言,门面仅仅是另外一个客户端而已。
一个系统可以有几个门面类
【GOF】的书中指出:在门面模式中,通常只需要一个门面类,并且此门面类只有一个实例,换言之它是一个单例类。当然这并不意味着在整个系统里只能有一个门面类,而仅仅是说对每一个子系统只有一个门面类。或者说,如果一个系统有好几个子系统的话,每一个子系统有一个门面类,整个系统可以有数个门面类。
为子系统增加新行为
初学者往往以为通过继承一个门面类便可在子系统中加入新的行为,这是错误的。因为这并不是门面模式的用意所在,门面模式的用意是为子系统提供一个集中化和简化的沟通管道,而不是有了一个新的添加新的行为的渠道。
例子:
如上图所示,一个警报系统using System;
using System;public class Camera{ public void TurnOn() { Console.WriteLine("Turning on the camera."); } public void TurnOff() { Console.WriteLine("Turning off the camera."); } public void Rotate(int degrees) { Console.WriteLine("Rotating the camera by {0} degrees.", degrees); }}public class Light{ public void TurnOff() { Console.WriteLine("Turning on the light."); } public void TurnOn() { Console.WriteLine("Turning off the light."); } public void ChangeBulb() { Console.WriteLine("changing the light-bulb."); }}public class Sensor{ public void Activate() { Console.WriteLine("Activating the sensor."); } public void Deactivate() { Console.WriteLine("Deactivating the sensor."); } public void Trigger() { Console.WriteLine("The sensor has triggered."); }}public class Alarm{ public void Activate() { Console.WriteLine("Activating the alarm."); } public void Deactivate() { Console.WriteLine("Deactivating the alarm."); } public void Ring() { Console.WriteLine("Ringing the alarm."); } public void StopRing() { Console.WriteLine("Stop the alarm."); }}public class SecurityFacade{ private static Camera camera1, camera2; private static Light light1, light2, light3; private static Sensor sensor; private static Alarm alarm; static SecurityFacade() { camera1 = new Camera(); camera2 = new Camera(); light1 = new Light(); light2 = new Light(); light3 = new Light(); sensor = new Sensor(); alarm = new Alarm(); } public void Activate() { camera1.TurnOn(); camera2.TurnOn(); light1.TurnOn(); light2.TurnOn(); light3.TurnOn(); sensor.Activate(); alarm.Activate(); } public void Deactivate() { camera1.TurnOff(); camera2.TurnOff(); light1.TurnOff(); light2.TurnOff(); light3.TurnOff(); sensor.Deactivate(); alarm.Deactivate(); }}public class Client{ private static SecurityFacade security; public static void Main( string[] args ) { security = new SecurityFacade(); security.Activate(); Console.WriteLine("\n--------------------\n"); security.Deactivate(); }}就可以方便的让client通过system进行警报系统的管理。
10. 桥接模式(bridge)
- 23种设计模式之我见----结构型设计模式之2(3)
- 23种设计模式之我见----结构型设计模式之2(3)
- 23种设计模式之我见----结构型设计模式(2)
- 23种设计模式之我见----结构型设计模式(2)
- 设计模式之我见
- 设计模式之我见
- 设计模式之我见
- 设计模式之我见
- 设计模式 之我见
- 设计模式之我见
- 设计模式之我见--3 装饰模式
- 设计模式之我见--2 策略模式
- 23种设计模式之我见(1)
- 设计模式这扇窗之我见
- 23种设计模式之我见----创建型模式(1)
- 23种设计模式之我见----创建型模式(1)
- 设计模式之我见(一)
- 设计模式之我见(二)
- ubuntu sublime 破解
- POJ-1579-Function Run Fun
- ACM-ICPC 2013 World Finals 简单题解
- 链表的翻转合并创建
- 大牛BLOG
- 23种设计模式之我见----结构型设计模式之2(3)
- 排序-计数排序
- 博客搬家
- 多线程、多核
- Linkify
- SearchManager
- TO READ 技术类
- JAVA SAX 方式解析XML文件
- 字符串专项:LA 4126