动机:

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

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

意图:

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

——《设计模式》GoF

结构:

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

 

代码结构

using System;
   2:  
namespace GangOfFour.Facade.Structural
   4: {
/// <summary>
/// MainApp startup class for Structural
/// Facade Design Pattern.
/// </summary>
class MainApp
  10:   {
/// <summary>
/// Entry point into console application.
/// </summary>
void Main()
  15:     {
new Facade();
  17:  
  18:       facade.MethodA();
  19:       facade.MethodB();
  20:  
// Wait for user
  22:       Console.ReadKey();
  23:     }
  24:   }
  25:  
/// <summary>
/// The 'Subsystem ClassA' class
/// </summary>
class SubSystemOne
  30:   {
void MethodOne()
  32:     {
);
  34:     }
  35:   }
  36:  
/// <summary>
/// The 'Subsystem ClassB' class
/// </summary>
class SubSystemTwo
  41:   {
void MethodTwo()
  43:     {
);
  45:     }
  46:   }
  47:  
/// <summary>
/// The 'Subsystem ClassC' class
/// </summary>
class SubSystemThree
  52:   {
void MethodThree()
  54:     {
);
  56:     }
  57:   }
  58:  
/// <summary>
/// The 'Subsystem ClassD' class
/// </summary>
class SubSystemFour
  63:   {
void MethodFour()
  65:     {
);
  67:     }
  68:   }
  69:  
/// <summary>
/// The 'Facade' class
/// </summary>
class Facade
  74:   {
private SubSystemOne _one;
private SubSystemTwo _two;
private SubSystemThree _three;
private SubSystemFour _four;
  79:  
public Facade()
  81:     {
new SubSystemOne();
new SubSystemTwo();
new SubSystemThree();
new SubSystemFour();
  86:     }
  87:  
void MethodA()
  89:     {
);
  91:       _one.MethodOne();
  92:       _two.MethodTwo();
  93:       _four.MethodFour();
  94:     }
  95:  
void MethodB()
  97:     {
);
  99:       _two.MethodTwo();
 100:       _three.MethodThree();
 101:     }
 102:   }
 103: }

相关文章:

  • 2022-01-19
  • 2021-10-09
  • 2021-07-07
  • 2022-03-03
  • 2021-10-24
  • 2022-12-23
  • 2021-07-06
猜你喜欢
  • 2021-05-28
  • 2021-08-02
  • 2022-01-08
  • 2021-06-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
相关资源
相似解决方案