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


    名称 Facade
    结构
    意图 为子系统中的一组接口提供一个一致的界面,F a c a d e 模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
    适用性
    • 当你要为一个复杂子系统提供一个简单接口时。子系统往往因为不断演化而变得越来越复杂。大多数模式使用时都会产生更多更小的类。这使得子系统更具可重用性,也更容易对子系统进行定制,但这也给那些不需要定制子系统的用户带来一些使用上的困难。F a c a d e 可以提供一个简单的缺省视图,这一视图对大多数用户来说已经足够,而那些需要更多的可定制性的用户可以越过f a c a d e 层。
    • 客户程序与抽象类的实现部分之间存在着很大的依赖性。引入f a c a d e 将这个子系统与客户以及其他的子系统分离,可以提高子系统的独立性和可移植性。
    • 当你需要构建一个层次结构的子系统时,使用f a c a d e 模式定义子系统中每层的入口点。如果子系统之间是相互依赖的,你可以让它们仅通过f a c a d e 进行通讯,从而简化了它们之间的依赖关系。
    Code Example
     1// Facade
     2
     3// Intent: "Provide a unified interface to a set of interfaces in a subsystem.
     4// Facade defines a higher-level interface that makes the subsystem easier
     5// to use". 
     6
     7// For further information, read "Design Patterns", p185, Gamma et al.,
     8// Addison-Wesley, ISBN:0-201-63361-2
     9
    10/* Notes:
    11 * Many subsystems are complex to manage - and this is a dis-incentive for 
    12 * client code to use them. A Facasde design pattern provides a simlified 
    13 * high-level API to the client, thus shielding it from direct contact 
    14 * with the (more complex) subsystem classes.  
    15 * 
    16 * Often the code that is inside a facade would have to be inside client 
    17 * code without the facade. The subsystem code beneath the facade can 
    18 * change, without affecting the client code. 
    19 */

    20 
    21namespace Facade_DesignPattern
    22{
    23    using System;
    24
    25    class SubSystem_class1 
    26    {
    27        public void OperationX() 
    28        {
    29            Console.WriteLine("SubSystem_class1.OperationX called");
    30        }

    31    }

    32
    33    class SubSystem_class2
    34    {
    35        public void OperationY()
    36        {
    37            Console.WriteLine("SubSystem_class2.OperationY called");
    38        }

    39    }

    40
    41    class SubSystem_class3 
    42    {
    43        public void OperationZ()
    44        {            
    45            Console.WriteLine("SubSystem_class3.OperationZ called");
    46        }
        
    47    }

    48
    49    class Facade 
    50    {
    51        private SubSystem_class1 c1 = new SubSystem_class1();
    52        private SubSystem_class2 c2 = new SubSystem_class2();
    53        private SubSystem_class3 c3 = new SubSystem_class3();
    54
    55        public void OperationWrapper()
    56        {
    57            Console.WriteLine("The Facade OperationWrapper carries out complex decision-making");
    58            Console.WriteLine("which in turn results in calls to the subsystem classes");
    59            c1.OperationX();
    60            if (1==1 /*some really complex decision*/)
    61            {
    62                c2.OperationY();
    63            }

    64            // lots of complex code here . . .
    65            c3.OperationZ();
    66        }

    67        
    68    }

    69
    70    /// <summary>
    71    ///    Summary description for Client.
    72    /// </summary>

    73    public class Client
    74    {
    75          public static int Main(string[] args)
    76        {
    77            Facade facade = new Facade();
    78            Console.WriteLine("Client calls the Facade OperationWrapper");
    79            facade.OperationWrapper();      
    80            return 0;
    81        }

    82    }

    83}

    84
    85

  • 相关阅读:
    nginx解决跨域问题
    SSM整合相关试题
    SSM整合案例--用户登录
    非法用户登录拦截
    SpringMVC拦截器和数据校验
    SpringMVC文件上传
    SpringMVC异常处理
    SpringMVC方法的返回值类型和自动装配
    SpringMVC
    spring和mybatis整合
  • 原文地址:https://www.cnblogs.com/DarkAngel/p/183725.html
Copyright © 2011-2022 走看看