打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Facade模式 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("\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 MethodMethodB() ---- SubSystemTwo Method SubSystemThree Method请按任意键继续. . .

4、外观模式应用分析

  外观模式适用情形:

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

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

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

  外观模式特点:

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

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

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

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

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

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

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#设计模式(11)——外观模式(Facade Pattern)
设计模式之☞外观模式
Java设计模式-外观(门面)模式
设计模式学习笔记(十一)——Facade外观模式
深入浅出外观模式(一)
设计模式---外观模式(Facade Pattern)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服