php专区

 首页 > php专区 > PHP进阶 > 设计模式 > 设计模式中的外观模式(Facade Pattern)

设计模式中的外观模式(Facade Pattern)

分享到:
【字体:
导读:
         摘要:在软件开发系统中,客户程序经常会与复杂系统的内部子系统之间产生耦合,而导致客户程序随着子系统的变化而变化。那么如何简化客户程序与子系统之间的交互接口?如何将复杂系统的内部子系统与客户程序之间的依赖解耦...

设计模式中的外观模式(Facade Pattern)

动机(Motivate):


    在软件开发系统中,客户程序经常会与复杂系统的内部子系统之间产生耦合,而导致客户程序随着子系统的变化而变化。那么如何简化客户程序与子系统之间的交互接口?如何将复杂系统的内部子系统与客户程序之间的依赖解耦?


意图(Intent):
    为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
                                                                         --------《设计模式》GOF

                              
            
适用性:

1.为一个复杂子系统提供一个简单接口。

2.提高子系统的独立性。

3.在层次化结构中,可以使用Facade模式定义系统中每一层的入口。
               


代码实现:


我们平时的开发中其实已经不知不觉的在用Fa?ade模式,现在来考虑这样一个抵押系统,当有一个客户来时,有如下几件事情需要确认:到银行子系统查询他是否有足够多的存款,到信用子系统查询他是否有良好的信用,到贷款子系统查询他有无贷款劣迹。只有这三个子系统都通过时才可进行抵押。我们先不考虑Fa?ade模式,那么客户程序就要直接访问这些子系统,分别进行判断。 
                 
    
在这个程序中,我们首先要有一个顾客类,它是一个纯数据类,并无任何操作,示意代码:

 1 //顾客类
 2 public class Customer
 3 {
 4     private string _name;
 5 
 6     public Customer(string name)
 7     {
 8         this._name = name;
 9     }
10 
11     public string Name
12     {
13         get { return _name; }
14     }
15 }


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

 1 //银行子系统
 2 public class Bank
 3 {
 4     public bool HasSufficientSavings(Customer c, int amount)
 5     {
 6         Console.WriteLine("Check bank for " + c.Name);
 7         return true;
 8     }
 9 }
10 
11 //信用子系统
12 public class Credit
13 {
14     public bool HasGoodCredit(Customer c)
15     {
16         Console.WriteLine("Check credit for " + c.Name);
17         return true;
18     }
19 }
20 
21 //贷款子系统
22 public class Loan
23 {
24     public bool HasNoBadLoans(Customer c)
25     {
26         Console.WriteLine("Check loans for " + c.Name);
27         return true;
28     }
29 }


看客户程序的调用:

 1 //客户程序
 2 public class MainApp
 3 {
 4     private const int _amount = 12000;
 5 
 6     public static void Main()
 7     {
 8         Bank bank = new Bank();
 9         Loan loan = new Loan();
10         Credit credit = new Credit();
11 
12         Customer customer = new Customer("Ann McKinsey");
13 
14         bool eligible = true;
15 
16         if (!bank.HasSufficientSavings(customer, _amount))
17         {
18             eligible = false;
19         }
20         else if (!loan.HasNoBadLoans(customer))
21         {
22             eligible = false;
23         }
24         else if (!credit.HasGoodCredit(customer))
25         {
26             eligible = false;
27         }
28 
29         Console.WriteLine("n" + customer.Name + " has been " + (eligible ? "Approved" : "Rejected"));
30         Console.ReadLine();
31     }
32 }


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

外观类Mortage的实现如下:

 1 /外观类
 2 public class Mortgage
 3 {
 4     private Bank bank = new Bank();
 5     private Loan loan = new Loan();
 6     private Credit credit = new Credit();
 7 
 8     public bool IsEligible(Customer cust, int amount)
 9     {
10         Console.WriteLine("{0} applies for {1:C} loann",
11           cust.Name, amount);
12 
13         bool eligible = true;
14 
15         if (!bank.HasSufficientSavings(cust, amount))
16         {
17             eligible = false;
18         }
19         else if (!loan.HasNoBadLoans(cust))
20         {
21             eligible = false;
22         }
23         else if (!credit.HasGoodCredit(cust))
24         {
25             eligible = false;
26         }
27 
28         return eligible;
29     }
30 }

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

 1 //银行子系统
 2 public class Bank
 3 {
 4     public bool HasSufficientSavings(Customer c, int amount)
 5     {
 6         Console.WriteLine("Check bank for " + c.Name);
 7         return true;
 8     }
 9 }
10 
11 //信用证子系统
12 public class Credit
13 {
14     public bool HasGoodCredit(Customer c)
15     {
16         Console.WriteLine("Check credit for " + c.Name);
17         return true;
18     }
19 }
20 
21 //贷款子系统
22 public class Loan
23 {
24     public bool HasNoBadLoans(Customer c)
25     {
26         Console.WriteLine("Check loans for " + c.Name);
27         return true;
28     }
29 }
30 
31 //顾客类
32 public class Customer
33 {
34     private string name;
35 
36     public Customer(string name)
37     {
38         this.name = name;
39     }
40 
41     public string Name
42     {
43         设计模式中的外观模式(Facade Pattern)

分享到:
装饰模式(Decorator Pattern) 详解
装饰模式(Decorator Pattern) 详解 子类复子类,子类何其多     假如我们需要为游戏中开发一种坦克,除了各种不同型号的坦克外,我们还希望在不同场合中为其增加以下一种或多种功能;比如红外线夜视功能,比如水陆两栖功能,比如卫星定位功能等等。按类继承的作法如下:    1  //抽象坦克2  publi...
设计模式之享元模式(Flyweight Pattern)
设计模式之享元模式(Flyweight Pattern)面向对象的代价 面向对象很好地解决了系统抽象性的问题,同时在大多数情况下,也不会损及系统的性能。但是,在某些特殊的应用中下,由于对象的数量太大,采用面向对象会给系统带来难以承受的内存开销。比如:图形应用中的图元等对象、字处理应用中的字符对象等。      ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……