• C#设计模式系列:外观模式(Facade)


    1. 外观模式简介

      外观模式主要解决的问题:当有多个类要处理时,需要一个个类去调用,没有复用性和扩展性。外观模式将处理子类的过程封装成操作,简化客户端的调用过程。

    1.1 定义

      外观模式(Facade)通过提供一个统一接口,来访问子系统的多个接口。

      使用外观模式时,创建一个统一的类,用来包装子系统中一个或多个复杂的类,客户端可以直接通过外观类来调用内部子系统中方法。

      外观模式让客户端与子系统之间避免紧耦合。

    1.2 使用频率

      

    2. 外观模式结构

    2.1 结构图

     

    2.2>、参与者

      外观模式参与者:

      ◊ Facade

        ° 知道哪些子系统类负责处理请求

        ° 将客户的请求代理给相应的子系统对象

      ◊ Subsystem Classes

        ° 实现子系统的功能

        ° 处理由Facade对象指派的任务来协调子系统下各子类的调用方式

      在外观模式中,外观类Facade的方法OptionWrapper实现的就是以不同的次序调用下面类SubSystem1、SubSystem2的方法Operation,通过不同的Operation组合实现装饰功能。

    3、外观模式结构实现

      SubSystemOne.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.FacadePattern.Structural
    {
        public class SubSystemOne
        {
            public void MethodOne()
            {
                Console.WriteLine(" SubSystemOne Method");
            }
        }
    }

      SubSystemTwo.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.FacadePattern.Structural
    {
        public class SubSystemTwo
        {
            public void MethodTwo()
            {
                Console.WriteLine(" SubSystemTwo Method");
            }
        }
    }

      SubSystemThree.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.FacadePattern.Structural
    {
        public class SubSystemThree
        {
            public void MethodThree()
            {
                Console.WriteLine(" SubSystemThree Method");
            }
        }
    }

      SubSystemFour.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.FacadePattern.Structural
    {
        public class SubSystemFour
        {
            public void MethodFour()
            {
                Console.WriteLine(" SubSystemFour Method");
            }
        }
    }

      Facade.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace DesignPatterns.FacadePattern.Structural
    {
        public class Facade
        {
            private SubSystemOne _one;
            private SubSystemTwo _two;
            private SubSystemThree _three;
            private SubSystemFour _four;
    
            public Facade()
            {
                _one = new SubSystemOne();
                _two = new SubSystemTwo();
                _three = new SubSystemThree();
                _four = new SubSystemFour();
            }
    
            public void MethodA()
            {
                Console.WriteLine("
    MethodA() ---- ");
                _one.MethodOne();
                _two.MethodTwo();
                _four.MethodFour();
            }
    
            public void MethodB()
            {
                Console.WriteLine("
    MethodB() ---- ");
                _two.MethodTwo();
                _three.MethodThree();
            }
        }
    }

      Program.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using DesignPatterns.FacadePattern.Structural;
    
    namespace DesignPatterns.FacadePattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                Facade facade = new Facade();
    
                facade.MethodA();
                facade.MethodB();
            }
        }
    }

      运行输出:

    MethodA() ----
     SubSystemOne Method
     SubSystemTwo Method
     SubSystemFour Method
    
    MethodB() ----
     SubSystemTwo Method
     SubSystemThree Method
    请按任意键继续. . .

    4、外观模式应用分析

      外观模式适用情形:

      ◊ 当要为一个复杂子系统提供一个简单接口时,子系统往往因为不断演化而变得越来越复杂。大多数模式使用时都会产生更多更小的类,这使得子系统更具可用性,也更容易对子系统进行定制。但这也给那些不需要定制子系统的用户带来一些使用上的困难。外观模式可以提供一个简单的默认视图。这一视图对大多数用户来说已经足够,而那些需要更多定制性的用户可以越过Facade层。

      ◊ 客户程序与抽象类的实现部分之间存在着很大的依赖性。引入外观模式将这个子系统与客户以及其他子系统分离,可以提高该子系统的独立性和可移植性。

      ◊ 当需要构建有层次结构的子系统时,使用外观模式定义每层的入口点。如果子系统间相互依赖,它们只需通过外观进行通信,从而简化它们之间的依赖关系,

      外观模式特点:

      ◊ 它实现了子系统对客户的屏蔽,因而减少了客户处理的对象数目并且使子系统使用起来更加方便。

      ◊ 它实现了子系统与客户之间的松耦合关系。而子系统内部的功能组件往往是紧耦合的。松耦合关系使得子系统的组件变化不会影响到它的客户。

      ◊ 外观模式有助于建立系统的层次结构,也有助于对对象之间的依赖关系分层。

      ◊ 外观模式可以消除复杂的循环依赖关系。这一点在客户程序与子系统是分别实现的时候尤为重要。在大型软件系统中降低编译依赖性至关重要。在子系统类改变时,希望尽量减少重编译工作以节省时间。

      ◊ 用外观模式可以降低编译依赖性,减少对重要系统做较小的改变所需的重编译工作。

      ◊ 外观模式有利于简化系统在不同平台之间的移植过程。因为编译一个子系统一般不需要编译所有其他子系统。

      ◊ 如果应用需要,外观模式并不限制子系统类的使用。因此可以在系统易用性和通用性之间加以选择。

  • 相关阅读:
    16:最长单词2
    18:Tomorrow never knows?
    备份裸设备上的数据文件
    几个Uboot命令
    Timus1132(二次剩余方程求解)
    Android 批量上传sd卡图片
    SVN:分支合并到主干
    Mac开发者必备实用工具推荐
    Solution for "De-serialization exception: Unable to find assembly xxxxx"
    UVA 10706 Number Sequence (找规律 + 打表 + 查找)
  • 原文地址:https://www.cnblogs.com/libingql/p/3636939.html
Copyright © 2020-2023  润新知