• 设计模式学习之路——Facade 外观模式(结构型模式)


    动机:

    组件的客户和组件中各种复杂的子系统有了过多的耦合,随着外部客户程序和各子系统的演化,这种过多的耦合面临很多变化的挑战。如何简化外部客户程序和系统间的交互接口?如何将外部客户程序的演化和内部子系统的变化之间的依赖相互解耦?

    clip_image002

    意图:

    为子系统中的一组接口提供一个一致的界面,Façade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

    ——《设计模式》GoF

    结构:

    clip_image004

    代码结构

       1: using System;
       2:  
       3: namespace GangOfFour.Facade.Structural
       4: {
       5:   /// <summary>
       6:   /// MainApp startup class for Structural
       7:   /// Facade Design Pattern.
       8:   /// </summary>
       9:   class MainApp
      10:   {
      11:     /// <summary>
      12:     /// Entry point into console application.
      13:     /// </summary>
      14:     public static void Main()
      15:     {
      16:       Facade facade = new Facade();
      17:  
      18:       facade.MethodA();
      19:       facade.MethodB();
      20:  
      21:       // Wait for user
      22:       Console.ReadKey();
      23:     }
      24:   }
      25:  
      26:   /// <summary>
      27:   /// The 'Subsystem ClassA' class
      28:   /// </summary>
      29:   class SubSystemOne
      30:   {
      31:     public void MethodOne()
      32:     {
      33:       Console.WriteLine(" SubSystemOne Method");
      34:     }
      35:   }
      36:  
      37:   /// <summary>
      38:   /// The 'Subsystem ClassB' class
      39:   /// </summary>
      40:   class SubSystemTwo
      41:   {
      42:     public void MethodTwo()
      43:     {
      44:       Console.WriteLine(" SubSystemTwo Method");
      45:     }
      46:   }
      47:  
      48:   /// <summary>
      49:   /// The 'Subsystem ClassC' class
      50:   /// </summary>
      51:   class SubSystemThree
      52:   {
      53:     public void MethodThree()
      54:     {
      55:       Console.WriteLine(" SubSystemThree Method");
      56:     }
      57:   }
      58:  
      59:   /// <summary>
      60:   /// The 'Subsystem ClassD' class
      61:   /// </summary>
      62:   class SubSystemFour
      63:   {
      64:     public void MethodFour()
      65:     {
      66:       Console.WriteLine(" SubSystemFour Method");
      67:     }
      68:   }
      69:  
      70:   /// <summary>
      71:   /// The 'Facade' class
      72:   /// </summary>
      73:   class Facade
      74:   {
      75:     private SubSystemOne _one;
      76:     private SubSystemTwo _two;
      77:     private SubSystemThree _three;
      78:     private SubSystemFour _four;
      79:  
      80:     public Facade()
      81:     {
      82:       _one = new SubSystemOne();
      83:       _two = new SubSystemTwo();
      84:       _three = new SubSystemThree();
      85:       _four = new SubSystemFour();
      86:     }
      87:  
      88:     public void MethodA()
      89:     {
      90:       Console.WriteLine("
    MethodA() ---- ");
      91:       _one.MethodOne();
      92:       _two.MethodTwo();
      93:       _four.MethodFour();
      94:     }
      95:  
      96:     public void MethodB()
      97:     {
      98:       Console.WriteLine("
    MethodB() ---- ");
      99:       _two.MethodTwo();
     100:       _three.MethodThree();
     101:     }
     102:   }
     103: }

    Output

    $X[7B2UN{)CX{II5YX0@)07

    Facade模式的几个要点

    • 从客户程序的角度来看, Facade模式不仅简化了整个组件系统的接口,同时对于组件内部与外部客户程序来说,从某种程度上也达到了一种“解耦”的效果——内部子系统的任何变化不会影响到Façade接口的变化。

    • Façade设计模式更注重从架构的层次去看整个系统,而不是单个类的层次。Façade很多时候更是一种架构设计模式。

    • 注意区分Façade模式、Adapter模式、Bridge模式与Decorator模式。Façade模式注重简化接口,Adapter模式注重转换接口,Bridge模式注重分离接口(抽象)与其实现,Decorator模式注重稳定接口的前提下为对象扩展功能。

  • 相关阅读:
    被标记为事务的方法互相调用的坑(上)
    几种实现延时任务的方式(三)
    几种实现延时任务的方式(二)
    几种实现延时任务的方式(一)
    Windows AD日志分析平台WatchAD安装教程
    Django单元测试中Fixtures用法
    威联通(NAS)搭建个人图床
    centOS极简安装并启动ngnix
    【JS档案揭秘】第一集 内存泄漏与垃圾回收
    【JS简洁之道小技巧】第一期 扁平化数组
  • 原文地址:https://www.cnblogs.com/luoht/p/3632900.html
Copyright © 2020-2023  润新知