• 外观模式


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

    举例如下:

    某个子系统的实现:

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

  • 相关阅读:
    POJ 1948 Triangular Pastures
    2018ACM/ICPC 青岛现场赛 E题 Plants vs. Zombies
    三大博弈
    ACM-ICPC 2018年北京网络赛 D-80 days
    hdu 2062 Subset sequence
    转-利用 Python 练习数据挖掘
    内联函数
    C++中冒号(:)和双冒号(::)的用法
    理性,感性和爱
    修改IE8搜索框为指定搜索引擎,如CSDN、百度知道等
  • 原文地址:https://www.cnblogs.com/angela217/p/5408445.html
Copyright © 2020-2023  润新知