分享

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

 雪柳花明 2016-09-10

  外观模式主要解决的问题是:当我们有多个类要处理时,往往要一个类一个类地区调用,没有复用性和扩展性。外观模式通过定义一个界面,把处理子类的过程封装成操作,主要就把用户从复杂的调用过程中解放出来。

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("\nMethodA() ---- ");
            _one.MethodOne();
            _two.MethodTwo();
            _four.MethodFour();
        }

        public void MethodB()
        {
            Console.WriteLine("\nMethodB() ---- ");
            _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层。

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

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

  外观模式特点:

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

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

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

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

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

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

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多