Facade Pattern简单随笔

简化接口:Facade模式在操作集成中的应用
本文探讨了Facade模式如何简化客户程序与复杂子系统之间的交互接口,通过将多个子系统操作集成到一个类中,实现操作的简化与方便性。详细介绍了操作集成的实现方式及其实例代码,展示了外观模式在简化复杂操作流程中的优势。

         Facade模式注重的是简化接口,为一个复杂子系统提供一个简单接口。 Facade Pattern:简化客户程序与子系统之间的交互接口,将复杂系统的内部子系统与客户程序之间的依赖解耦。 

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

         Facade模式对客户屏蔽了子系统组件,因而减少了客户处理的对象的数目并使得子系统使用起来更加方便。Facade模式实现了子系统与客户之间的松耦合关系,而子系统内部的功能组件往往是紧耦合的。松耦合关系使得子系统的组件变化不会影响到它的客户。

 

对于操作A,B,C而言,如果仅仅通过客户端来操作数据对象,将生成A,B,C相应的对象(完全由客户自己创建),比较繁琐。

 

通过将操作A,B,C的操作隐藏到FacadeOperater类中进行相应的操作,提供给客户端的就仅仅是一个方法而已,简单,方便,易于维护。

也就是说将A,B,C的操作集成到FacadeOperater类中。

A,B,C代码如下:

 

 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Text;
 4 
 5  namespace  FacadePatternSam
 6  {
 7       public   class  OperaterA
 8      {
 9           private   bool  result  =   false ;
10           // 判断用户的名字长度是否大于5
11           public   bool  VerufyName(ClientData obj) 
12          {
13               if  (obj.Name.Length  >   5 )
14              {
15                  result  =   true ;
16              }
17               else  
18              {
19                  Console.WriteLine( " name length is  lesser than 5! " );
20              }
21              return  result;
22          }
23      }
24  }
25 
26 
27  using  System;
28  using  System.Collections.Generic;
29  using  System.Text;
30 
31  namespace  FacadePatternSam
32  {
33       public   class  OperaterB
34      {
35           private   bool  result  =   false ;
36           public   bool  VerifyAge(ClientData obj)
37          {
38               if  (obj.Age  >   50 )
39              {
40                  result  =   true ;
41              }
42               else
43              {
44                  Console.WriteLine( " age is smaller than 50! " );
45              }
46               return  result;
47          }
48      }
49  }
50 
51 
52  using  System;
53  using  System.Collections.Generic;
54  using  System.Text;
55 
56  namespace  FacadePatternSam
57  {
58       public   class  OperaterC
59      {
60           private   bool  result  =   false ;
61           public   bool  VerifyNumber(ClientData obj)
62          {
63               if  (obj.Number  >   60 )
64              {
65                  result  =   true ;
66              }
67               else
68              {
69                  Console.WriteLine( " number is smaller than 60 " );
70 
71              }
72               return  result;
73          }
74      }
75  }
76 

 

 

将这3个类的操作集成;

 

 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Text;
 4 
 5  namespace  FacadePatternSam
 6  {
 7       public   class  FacadeOperater
 8      {
 9           private   bool  result  =   false ;
10          OperaterA a  =   new  OperaterA();
11          OperaterB b  =   new  OperaterB();
12          OperaterC c  =   new  OperaterC();
13          
14           public   bool  VerifyAll(ClientData obj)
15          {
16               if  (a.VerufyName(obj)  &&  b.VerifyAge(obj)  &&  c.VerifyNumber(obj))
17              {
18                  result  =   true ;
19                  Console.WriteLine( " operater  is ok !! " );
20              }
21               else  
22              {
23                  Console.WriteLine( " please do again!It is not rigjt for your  operation! " );
24              
25              }
26               return  result;
27          }
28      }
29  }
30 

 

操作数据为:

 

 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Text;
 4 
 5  namespace  FacadePatternSam
 6  {
 7       public   class  ClientData
 8      {
 9           private   string  _name;
10           private   float  _age;
11           private   float  _number;
12           public  ClientData( string  name,  float  age, float  number)
13          {
14               this ._age  =  age;
15               this ._name  =  name;
16               this ._number  =  number;
17 
18          }
19           public   string  Name
20          {
21               get
22              {
23                   return  _name;
24              }
25          }
26 
27           public   float  Age
28          {
29               get
30              {
31                   return  _age;
32              }
33          }
34 
35           public   float  Number
36          {
37               get  
38              {
39                   return  _number;
40              }
41          
42          }
43      }
44  }
45 

 

 

客户端代码:一种是没外观模式的  一种为用这种模式的,当然 这个例子比较简单,代码量差不多

 

 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Text;
 4 
 5  namespace  FacadePatternSam
 6  {
 7       class  Program
 8      {
 9           static   void  Main( string [] args)
10          {
11               // 基本操作
12               ClientData clientObj  =   new  ClientData( " AAAAAA " , 60 , 70 );
13               OperaterA a  =   new  OperaterA();
14               OperaterB b  =   new  OperaterB();
15               OperaterC c  =   new  OperaterC();
16                if  (a.VerufyName(clientObj)  &&  b.VerifyAge(clientObj)  &&  c.VerifyNumber(clientObj))
17               {
18                   Console.WriteLine( " it  is  ok !!! " );
19               }
20                else  
21               {
22                   Console.WriteLine( " not  pass!! " );
23               }
24               Console.WriteLine();
25               Console.ReadLine();
26              
27               // 外观模式操作
28              FacadeOperater oper  =   new  FacadeOperater();
29              ClientData obj  =   new  ClientData( " yyyyyyyyyyy " 50 60 );
30               if  (oper.VerifyAll(obj))
31              {
32                  Console.WriteLine( " OK " );
33              }
34               else
35              {
36                  Console.WriteLine( " not pass! " );
37              }
38              Console.ReadLine();
39          }
40      }
41  }
42 

 

 

显示结果如下所示:

 

******************************************************************************

源代码下载:/Files/jasenkin/FacadePatternSam.rar

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值