门面模式(Facade Pattern)

一、什么是门面模式

门面模式要求一个子系统的外部与其内部的通信必须通过一个统一的门面(Facade)对象进行。门面模式提供一个高层次的接口,使得子系统更易于使用。

意图:

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

示意图:

门面模式(Facade Pattern)

 

 

二、门面模式的效果及实现要点

  • Façade模式对客户屏蔽了子系统组件,因而减少了客户处理的对象的数目并使得子系统使用起来更加方便。
  • Façade模式实现了子系统与客户之间的松耦合关系,而子系统内部的功能组件往往是紧耦合的。松耦合关系使得子系统的组件变化不会影响到它的客户。
  • 如果应用需要,它并不限制它们使用子系统类。因此你可以在系统易用性与通用性之间选择。

 

 

三、门面模式的适用性

  • 为一个复杂子系统提供一个简单接口。
  • 提高子系统的独立性。
  • 在层次化结构中,可以使用Facade模式定义系统中每一层的入口。

 

 

四、门面模式的实例

需求:现在来考虑这样一个抵押系统,当有一个客户来时,有如下几件事情需要确认:到银行子系统查询他是否有足够多的存款,到信用子系统查询他是否有良好的信用,到贷款子系统查询他有无贷款劣迹。只有这三个子系统都通过时才可进行抵押。

我们先不考虑Façade模式,那么客户程序就要直接访问这些子系统,分别进行判断。类结构图下:

门面模式(Facade Pattern)

在这个程序中,我们首先要有一个顾客类,它是一个纯数据类,并无任何操作,示意代码:

 1 public class Customer

 2 {

 3     private string name;

 4 

 5     public string Name

 6     {

 7         get 

 8         {

 9             return name;

10         }

11     }

12 

13     public Customer(string name)

14     {

15         this.name = name;

16     }

17 }

下面这三个类均是子系统类,示意代码:

 1 public class Bank

 2 {

 3     public bool HasSufficientSavings(Customer c, int amount)

 4     {

 5         Console.WriteLine("Check bank for " + c.Name);

 6         return true;

 7     }

 8 }

 9 

10 public class Credit

11 {

12     public bool HasGoodCredit(Customer c)

13     {

14         Console.WriteLine("Check credit for " + c.Name);

15         return true;

16     }

17 }

18 

19 public class Loan

20 {

21     public bool HasNoBadLoans(Customer c)

22     {

23         Console.WriteLine("Check loans for " + c.Name);

24         return true;

25     }

26 }

来看客户程序的调用:

 1 public class Program

 2 {

 3     private const int amount = 1200;

 4 

 5     public static void Main(string[] args)

 6     {

 7         Customer customer = new Customer("Steven Jobs");

 8 

 9         Bank bank = new Bank();

10         Credit credit = new Credit();

11         Loan loan = new Loan();

12 

13         bool eligible = true;

14 

15         if (!bank.HasSufficientSavings(customer, amount))

16         {

17             eligible = false;

18         }

19         else if (!loan.HasNoBadLoans(customer))

20         {

21             eligible = false;

22         }

23         else if (!credit.HasGoodCredit(customer))

24         {

25             eligible = false;

26         }

27 

28         Console.WriteLine("\n" + customer.Name + " has been " + (eligible ? "Approved" : "Rejected"));

29         Console.ReadLine();

30     }

31 }

可以看到,在不用Façade模式的情况下,客户程序与三个子系统都发生了耦合,这种耦合使得客户程序依赖于子系统,当子系统变化时,客户程序也将面临很多变化的挑战。一个合情合理的设计就是为这些子系统创建一个统一的接口,这个接口简化了客户程序的判断操作。看一下引入Façade模式后的类结构图:

门面模式(Facade Pattern)

门面类Mortage的实现如下:

 1 public class Mortgage

 2 {

 3     private Bank bank = new Bank();

 4     private Credit credit = new Credit();

 5     private Loan loan = new Loan();

 6 

 7     public bool IsEligible(Customer customer, int amount)

 8     {

 9         Console.WriteLine("{0} applies for {1:C} loan\n", customer.Name, amount);

10 

11         bool eligible = true;

12 

13         if (!bank.HasSufficientSavings(customer, amount))

14         {

15             eligible = false;

16         }

17         else if (!loan.HasNoBadLoans(customer))

18         {

19             eligible = false;

20         }

21         else if (!credit.HasGoodCredit(customer))

22         {

23             eligible = false;

24         }

25 

26         return eligible;

27     }

28 }

顾客类和子系统类的实现仍然如下:

 1 public class Customer

 2 {

 3     private string name;

 4 

 5     public string Name

 6     {

 7         get

 8         {

 9             return name;

10         }

11     }

12 

13     public Customer(string name)

14     {

15         this.name = name;

16     }

17 }

18 

19 public class Bank

20 {

21     public bool HasSufficientSavings(Customer c, int amount)

22     {

23         Console.WriteLine("Check bank for " + c.Name);

24         return true;

25     }

26 }

27 

28 public class Credit

29 {

30     public bool HasGoodCredit(Customer c)

31     {

32         Console.WriteLine("Check credit for " + c.Name);

33         return true;

34     }

35 }

36 

37 public class Loan

38 {

39     public bool HasNoBadLoans(Customer c)

40     {

41         Console.WriteLine("Check loans for " + c.Name);

42         return true;

43     }

44 }

而此时客户程序的实现:

 1 public class Program

 2 {

 3     private const int amount = 1200;

 4 

 5     public static void Main(string[] args)

 6     {

 7         Customer customer = new Customer("Steven Jobs");

 8         Mortgage mortgage = new Mortgage();

 9 

10         Console.WriteLine("\n" + customer.Name + " has been " + (mortgage.IsEligible(customer,amount) ? "Approved" : "Rejected"));

11         Console.ReadLine();

12     }

13 }

可以看到引入Façade模式后,客户程序只与Mortgage发生依赖,也就是Mortgage屏蔽了子系统之间的复杂的操作,达到了解耦内部子系统与客户程序之间的依赖。

你可能感兴趣的:(Pattern)