zoukankan      html  css  js  c++  java
  • 外观模式

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

    举例如下:

    某个子系统的实现:

         /// <summary>
        /// 子系统,实现自己的功能
        /// </summary>
        public class SubSystemOne
        {
            public void MethodOne()
            {
                Console.WriteLine("Method One");
            }
        }
    
        public class SubSystemTwo
        {
            public void MethodTwo()
            {
                Console.WriteLine("Method Two");
            }
        }
    
        public class SubSystemThree
        {
            public void MethodThree()
            {
                Console.WriteLine("Method Three");
            }
        }
    
        public class SubSystemFour
        {
            public void MethodFour()
            {
                Console.WriteLine("Method Four");
            }
        }
    

      外观模式实现:

        /// <summary>
        /// 外观类,它了解所有子系统的方法或属性,根据需要进行组合,以备外界调用
        /// </summary>
        public class Facade
        {
            SubSystemOne systemOne;
            SubSystemTwo systemTwo;
            SubSystemThree systemThree;
            SubSystemFour systemFour;
    
            public Facade()
            {
                systemOne = new SubSystemOne();
                systemTwo = new SubSystemTwo();
                systemFour = new SubSystemFour();
                systemThree = new SubSystemThree();
    
            }
    
    
            public void MethodA()
            {
                systemOne.MethodOne();
                systemThree.MethodThree();
            }
    
            public void MethodB()
            {
                systemTwo.MethodTwo();
                systemThree.MethodThree();
                systemFour.MethodFour();
            }
        }
    

      使用外观类:由于外观类的存在,客户端可以根本不知道子系统的存在,只需使用外观类方法即可

     Facade facade = new Facade();
     facade.MethodA();
     facade.MethodB();
    

      运行结果如下:

      Method One
      Method Three

      Method Two
      Method Three
      Method Four

    外观模式用途:

    1 在设计初期阶段,应该有意识的将不同的两个层分离,层与层之间建立外观,这样可以为复杂的子系统提供一个简单的接口,使得耦合大大降低

    2 在开发阶段,子系统往往因为不断地重构演化而变得越来越复杂,增加外观可以提供一个简单的接口,减少它们之间的依赖

    3 在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展,为新系统开发一个外观类,来提供比较清晰简单的接口,让新系统与外观交互,外观与遗留代码交互所有复杂工作

  • 相关阅读:
    数据库MySQL调优实战经验总结
    Apache常见功能实战详解
    使用HeartBeat实现高可用HA的配置过程详解
    Nginx实现集群的负载均衡配置过程详解
    CentOS系统通过PXE实现批量无人值守安装
    CentOS 7 网卡命名修改为eth0格式
    Nagios 系统监控基本安装配置过程详解
    LAMP 系统服务搭建过程详解
    使用 python 管理 mysql 开发工具箱
    C++标准库string类型的使用和操作总结
  • 原文地址:https://www.cnblogs.com/angela217/p/5408445.html
Copyright © 2011-2022 走看看