zoukankan      html  css  js  c++  java
  • 11.外观模式(Facade Pattern)

    using System;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            /// <summary>
            /// 不使用外观模式的情况
            /// 此时客户端与三个子系统都发送了耦合,使得客户端程序依赖与子系统
            /// 为了解决这样的问题,我们可以使用外观模式来为所有子系统设计一个统一的接口
            /// 客户端只需要调用外观类中的方法就可以了,简化了客户端的操作
            /// 从而让客户和子系统之间避免了紧耦合
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                SubSystemA a = new SubSystemA(); 
                SubSystemB b = new SubSystemB(); 
                SubSystemC c = new SubSystemC(); 
                a.MethodA(); 
                b.MethodB(); 
                c.MethodC(); 
                Console.Read();
            }
        }
    
        // 子系统A
        public class SubSystemA
        {
            public void MethodA()
            {
                Console.WriteLine("执行子系统A中的方法A");
            }
        }
    
        // 子系统B
        public class SubSystemB
        {
            public void MethodB()
            {
                Console.WriteLine("执行子系统B中的方法B");
            }
        }
    
        // 子系统C
        public class SubSystemC
        {
            public void MethodC()
            {
                Console.WriteLine("执行子系统C中的方法C");
            }
        }
    }
    using System;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            private static RegistrationFacade facade = new RegistrationFacade();
    
            /// <summary>
            /// 不使用外观模式的情况
            /// 此时客户端与三个子系统都发送了耦合,使得客户端程序依赖与子系统
            /// 为了解决这样的问题,我们可以使用外观模式来为所有子系统设计一个统一的接口
            /// 客户端只需要调用外观类中的方法就可以了,简化了客户端的操作
            /// 从而让客户和子系统之间避免了紧耦合
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                facade.RegisterSystem(1);
                facade.RegisterSystem(2);
                facade.RegisterSystem(3);
                Console.Read();
            }
        }
    
        public class RegistrationFacade
        {
            private SubSystemA RegistrationA;
            private SubSystemB RegistrationB;
            private SubSystemC RegistrationC;
            public RegistrationFacade()
            {
                RegistrationA = new SubSystemA();
                RegistrationB = new SubSystemB();
                RegistrationC = new SubSystemC();
            }
    
            public void RegisterSystem(int type)
            {
                if (type == 1)
                {
                    RegistrationA.MethodA();
                }
                else if (type == 2)
                {
                    RegistrationB.MethodB();
                }
                else if (type == 3)
                {
                    RegistrationC.MethodC();
                }
            }
        }
    
        // 子系统A
        public class SubSystemA
        {
            public void MethodA()
            {
                Console.WriteLine("执行子系统A中的方法A");
            }
        }
    
        // 子系统B
        public class SubSystemB
        {
            public void MethodB()
            {
                Console.WriteLine("执行子系统B中的方法B");
            }
        }
    
        // 子系统C
        public class SubSystemC
        {
            public void MethodC()
            {
                Console.WriteLine("执行子系统C中的方法C");
            }
        }
    }
  • 相关阅读:
    Math类四个常用方法辨析,floor、ceil、round、rint
    【转】ctypes库的使用整理
    【转】采用dlopen、dlsym、dlclose加载动态链接库【总结】
    【转】collectd的部署
    【转】Pycharm Professional(专业版)完美破解,
    【转】在结构体最后定义一个长度为0的字符数组(技巧)
    【转】C++11智能指针之weak_ptr
    轻量级、高性能http代理服务器,内网穿透从未如此简单。
    php 大文件上传 redis+php resque 较低io消耗
    leetcode 870. 优势洗牌
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4641673.html
Copyright © 2011-2022 走看看