C#设计模式--外观模式

0.C#设计模式--简单工厂模式

1.C#设计模式--工厂方法模式

2.C#设计模式--抽象工厂模式

3.C#设计模式--单例模式

4.C#设计模式--建造者模式

5.C#设计模式--原型模式

6.C#设计模式--适配器模式

7.C#设计模式--装饰器模式

8.C#设计模式--代理模式

设计模式:

外观模式(Facade Pattern)

简单介绍:

外观模式(Facade Pattern):

外观模式(Facade)的定义:为子系统中的一组接口提供一个一致的界面,用来访问子系统中的一群接口。

比如调用子系统1和子系统2的Print方法,如果没有外观模式那么你需要一个个的调用,子系统如果是少还好,多了就要疯了,这里就可以使用外观模式,调用外观模式对外提供的方法一次性完成对子系统的层层调用

生活实例:比如笔记本是一个整体,看似是一件东西,实际上只是将主机和显示器组装在一起而已,对外提供一个电源按钮,当按下电源按钮的时候,启动了主机和显示器,这里的这个电源按钮就差不多是外观模式。对外只提供了一个电源按钮。

外观模式组成:

Facade:负责子系统的的封装调用

Subsystem Classes:具体的子系统,实现由外观模式Facade对象来调用的具体任务

外观模式类图:

外观模式C#代码举例:

SubSystem1:子系统1

    /// <summary>
    /// 子系统1
    /// </summary>
    public class SubSystem1
    {
        public void Print1()
        {
            Console.WriteLine("SubSystem1");
        }
    }

SubSystem2:子系统2类

    /// <summary>
    /// 子系统2
    /// </summary>
    public class SubSystem2
    {
        /// <summary>
        /// 子系统2
        /// </summary>
        public void Print2()
        {
            Console.WriteLine("SybSystem2");
        }
    }

Facade:外观模式对外提供接口类

    /// <summary>
    ///  外观模式对外接口类
    /// </summary>
    public class Facade
    {
        private SubSystem1 _subSystem1;
        private SubSystem2 _subSystem2;

        public Facade()
        {
            _subSystem1 = new SubSystem1();
            _subSystem2 = new SubSystem2();
        }

        public void Print()
        {
            _subSystem1.Print1();
            _subSystem2.Print2();
        }
    }

用户测试类:

    /// <summary>
    /// 客户端测试类
    /// </summary>
    class Client
    {
        static void Main(string[] args)
        {
            Facade facade = new Facade();
            facade.Print();
            Console.Read();
        }
    }

运行结果:

外观模式生活实例笔记本例子:

生活实例:比如笔记本是一个整体,看似是一件东西,实际上只是将主机和显示器组装在一起而已,对外提供一个电源按钮,当按下电源按钮的时候,启动了主机和显示器,这里的这个电源按钮就差不多是外观模式。对外只提供了一个电源按钮。

假设笔记本是由来个子系统:显示器和主机组成

当按下电源按钮,开启主机和显示器

外观模式生活实例笔记本例子类图:


外观模式生活实例笔记本例子C#代码:

PC_Monitor:笔记本显示器类

    /// <summary>
    /// 笔记本显示器
    /// </summary>
    public class PC_Monitor
    {
        public void Open()
        {
            Console.WriteLine("开启显示器");
        }
        public void Close()
        {
            Console.WriteLine("关闭显示器");
        }
    }

PC_Host:笔记本主机类

    /// <summary>
    /// 笔记本主机
    /// </summary>
    public class PC_Host
    {
        public void Open()
        {
            Console.WriteLine("开启主机");
        }
        public void Close()
        {
            Console.WriteLine("关闭主机");
        }
    }

PC_Facade:笔记本外观模式对外接口类

    /// <summary>
    /// 笔记本对外接口类
    /// </summary>
    public class PC_Facade
    {
        private PC_Monitor _PC_Monitor;
        private PC_Host _PC_Host;

        public PC_Facade()
        {
            _PC_Monitor = new PC_Monitor();
            _PC_Host = new PC_Host();
        }

        public void Open()
        {
            Console.WriteLine("开机……");
            _PC_Monitor.Open();
            _PC_Host.Open();
        }

        public void Close()
        {
            Console.WriteLine("关机……");
            _PC_Host.Close();
            _PC_Monitor.Close();
        }
    }

用户测试代码:

    class Client
    {
        /// <summary>
        /// 用户测试类
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            PC_Facade pc_facade = new PC_Facade();
            //开机
            pc_facade.Open();
            //关机
            pc_facade.Close();
            Console.Read();
        }
    }

运行结果:

外观模式C#工程源码下载

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
创建型: 1. 单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype) 结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern) 17. 解释器模式(Interpreter Pattern) 18. 中介者模式(Mediator Pattern) 19. 职责链模式(Chain of Responsibility Pattern) 20. 备忘录模式(Memento Pattern) 21. 策略模式(Strategy Pattern) 22. 访问者模式(Visitor Pattern) 23. 状态模式(State Pattern) 工程结构 ├─01.Singleton │ ├─html │ └─MySingleton │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─02.ChainOfResponsibility │ ├─html │ ├─My2ChainOfResponsibility │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─MyChainOfResponsibility │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ ├─Refactor │ │ └─TempPE │ └─Properties ├─03.FactoryMethodMode │ ├─FactoryMethodMode │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─html ├─04.AbstractFactory │ ├─04.1.SimpleFactory │ │ ├─html │ │ └─SimpleFactory │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ ├─AbstractFactory │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ └─html ├─05.BuilderPattern │ ├─html │ └─MyBuilderPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─06.PrototypePattern │ ├─html │ │ └─C#设计模式(6)——原型模式(Prototype Patt O技术博客_files │ └─PrototypePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─07.AdapterPattern │ ├─html │ └─MyAdapterPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─08.BridgePattern │ ├─html │ └─MyBridgePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─09.DecoratorPattern │ ├─html │ └─MyDecoratorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─10.CompositePattern │ ├─html │ └─MyCompositePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─11.FacadePattern │ ├─html │ └─MyFacadePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─12.FlyweightPattern │ ├─html │ └─MyFlyweightPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─13.ProxyPattern │ ├─html │ └─MyProxyPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─14.TemplateMethod │ ├─html │ └─MyTemplateMethod │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─15.VisitorPattern │ ├─html │ └─MyVisitorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─16.StrategyPattern │ ├─html │ └─MyStrategyPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─17.StatePattern │ ├─html │ └─StatePattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─18.MementoPattern │ ├─html │ └─MementoPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─19.MediatorPattern │ ├─html │ └─MediatorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─20.OberverPattern │ ├─CatOberverPattern │ │ ├─bin │ │ │ └─Debug │ │ ├─obj │ │ │ └─Debug │ │ │ └─TempPE │ │ └─Properties │ ├─html │ └─MyOberverPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─21.IteratorPattern │ ├─html │ └─IteratorPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties ├─22.InterpreterPattern │ ├─html │ └─MyInterpreterPattern │ ├─bin │ │ └─Debug │ ├─obj │ │ └─Debug │ │ └─TempPE │ └─Properties └─23.CommandPattern ├─html └─MyCommandPattern ├─bin │ └─Debug ├─obj │ └─Debug │ └─TempPE └─Properties

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值