苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper万能框架(V2.3-含.netcore) HttpHelper官方出品,无敌框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 最牛的爬虫类,没有爬不到只有想不到 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 710|回复: 5

[学习心得] C#门面模式列子

[复制链接]
发表于 2019-1-8 13:58:26 | 显示全部楼层 |阅读模式
一 、  一个例子

我们考察一个保安系统的例子,以说明门面模式的功效。一个保安系统由两个录像机、三个电灯、一个遥感器和
一个警报器组成。保安系统的操作人员需要经常将这些仪器启动和关闭。

不使用门面模式的设计

首先,在不使用门面模式的情况下,操作这个保安系统的操作员必须直接操作所有的这些部件。下图所示就是在
不使用门面模式的情况下系统的设计图。
2.png

可以看出,Client 对象需要引用到所有的录像机(Camera)、电灯(Light)、感应器(Sensor)和警报器(Alarm)
对象。代码如下:

[C#] 纯文本查看 复制代码
using System;

namespace JobApi
{
    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 Client
    {
        private static readonly Camera camera1;
        private static readonly Camera camera2;
        private static readonly Light light1;
        private static readonly Light light2;
        private static readonly Light light3;
        private static readonly Sensor sensor;
        private static readonly Alarm alarm;
        static Client()
        {
            camera1 = new Camera();
            camera2 = new Camera();
            light1 = new Light();
            light2 = new Light();
            light3 = new Light();
            sensor = new Sensor();
            alarm = new Alarm();
        }
        public static void Main(string[] args)
        {
            camera1.TurnOn();
            camera2.TurnOn();
            light1.TurnOn();
            light2.TurnOn();
            light3.TurnOn();
            sensor.Activate();
            alarm.Activate();
        }
    }
}


二 、 使用门面模式的设计

一个合情合理的改进方法就是准备一个系统的控制台,作为保安系统的用户界面。如下图所示:


3.png

程序代码如下:

[C#] 纯文本查看 复制代码
using System;

namespace JobApi
{
    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 readonly Camera camera1;
        private static readonly Camera camera2;
        private static readonly Light light1;
        private static readonly Light light2;
        private static readonly Light light3;
        private static readonly Sensor sensor;
        private static readonly 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();
        }
    }
}






发表于 2019-1-8 14:29:26 | 显示全部楼层
发表于 2019-1-8 16:40:35 | 显示全部楼层
发表于 2019-1-8 16:43:07 | 显示全部楼层
发表于 2019-1-8 18:03:35 | 显示全部楼层
发表于 2019-1-8 21:04:18 | 显示全部楼层
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备17001017号-1)

GMT+8, 2019-12-7 23:07

© 2017-2018

快速回复 返回顶部 返回列表