详解.NET设计模式实例中的外观模式
创始人
2024-06-21 11:41:37
0

本文将介绍的是外观模式,也就是.NET设计模式中的一种。在这里希望通过本文,能让大家对.NET设计模式有进一步的了解,同时也提供了相应代码实例供大家参考。

#T#

一、外观模式简介(Brief Introduction)

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

二、解决的问题(What To Solve)

1、分离不同的两个层

典型的分层例子是Net三层架构,界面层与业务逻辑层分离,业务逻辑层与数据访问层分类。这样可以为子系统提供统一的界面和接口,降低了系统的耦合性。

2、减少依赖

随着功能增加及程序的重构,系统会变得越来越复杂,这时增加一个外观可以提供一个简单的接口,减少他们之间的依赖。

3、为新旧系统交互提供接口

有的时候,新系统需要旧系统的核心功能,而这个旧的系统已经很难维护和扩展,可以给新系统增加一个Façade类,是的新系统与Façade类交互,Façade类与旧系统交互素有复杂的工作。

三、外观模式分析(Analysis)

1、外观模式结构

2、源代码

1、子系统类SubSystemOne

  1. public class SubSystemOne  
  2. {  
  3.     public void MethodOne()  
  4.     {  
  5.         Console.WriteLine("执行子系统One中的方法One");  
  6.     }  

2、子系统类SubSystemTwo

  1. public class SubSystemTwo  
  2. {  
  3.     public void MethodTwo()  
  4.     {  
  5.         Console.WriteLine("执行子系统Two中的方法Two");  
  6.     }  

3、子系统类SubSystemThree

  1. public class SubSystemThree  
  2. {  
  3.     public void MethodThree()  
  4.     {  
  5.         Console.WriteLine("执行子系统Three中的方法Three");  
  6.     }  

4、Facade 外观类,为子系统类集合提供更高层次的接口和一致的界面

  1. public class Facade  
  2. {  
  3.     SubSystemOne one;  
  4.     SubSystemTwo two;  
  5.     SubSystemThree three;  
  6.     public Facade()  
  7.     {  
  8.        one = new SubSystemOne();  
  9.         two = new SubSystemTwo();  
  10.         three = new SubSystemThree();  
  11.     }  
  12.     public void MethodA()  
  13.     {  
  14.         Console.WriteLine("开始执行外观模式中的方法A");  
  15.         one.MethodOne();  
  16.         two.MethodTwo();  
  17.         Console.WriteLine("外观模式中的方法A执行结束");  
  18.         Console.WriteLine("---------------------------");  
  19.     }  
  20.     public void MethodB()  
  21.    {  
  22.         Console.WriteLine("开始执行外观模式中的方法B");  
  23.         two.MethodTwo();  
  24.         three.MethodThree();  
  25.         Console.WriteLine("外观模式中的方法B执行结束");  
  26.     }  

5、客户端代码

  1. static void Main(string[] args)  
  2. {  
  3.     Facade facade = new Facade();  
  4.     facade.MethodA();  
  5.     facade.MethodB();  
  6.     Console.Read();  
 

3、程序运行结果

运行结果

四.案例分析(Example)

1、场景

假设远程网络教育系统-用户注册模块包括功能有

1、验证课程是否已经满人

2、收取客户费用

3、通知用户课程选择成功

如下图所示

通知用户选择成功

子系统类集合包括:PaymentGateway类、RegisterCourse类、NotifyUser类

PaymentGateway类:用户支付课程费用

RegisterCourse类:验证所选课程是否已经满人以及计算课程的费用

NotifyUser类:" 用户选择课程成功与否"通知用户

RegistrationFacade类:外观类,提供一个统一的界面和接口,完成课程校验、网上支付、通知用户功能

2、代码

1、子系统类集合

  1. namespace FacadePattern    
  2.  {    
  3. ///     
  4. /// Subsystem for making financial transactions    
  5. ///     
  6. public class PaymentGateway    
  7. {    
  8. public bool ChargeStudent(string studentName, int costTuition)    
  9. {    
  10. //Charge the student    
  11. Console.WriteLine(String.Format("Charging student {0} for ${1}", studentName, costTuition.ToString()));    
  12. return true;    
  13. }    
  14. }    
  15. ///     
  16. /// Subsystem for registration of courses    
  17. ///     
  18. public class RegisterCourse    
  19. {    
  20.  public bool CheckAvailability(string courseCode)    
  21. {    
  22. //Verify if the course is available..    
  23.                Console.WriteLine(String.Format("Verifying availability of seats for the course : {0}", courseCode));    
  24.               return true;    
  25.            }    
  26.           public int GetTuitionCost(string courseCode)    
  27.            {    
  28.                //Get the cost of tuition    
  29.                return 1000;    
  30.            }    
  31.        }    
  32.       ///     
  33.        /// Subsystem for Notifying users    
  34.       ///     
  35.       public class NotifyUser    
  36.        {    
  37.           public bool Notify(string studentName)    
  38.           {    
  39.               //Get the name of the instructor based on Course Code    
  40.               //Notify the instructor    
  41.                Console.WriteLine("Notifying Instructor about new enrollment");    
  42.                return true;    
  43.           }    
  44.       }    
  45.    } 

2、外观类Façade Class

  1. ///     
  2. /// The Facade class that simplifies executing methods  
  3. in the subsystems and hides implementation for the client   
  4. ///     
  5. public class RegistrationFacade    
  6. {    
  7. private PaymentGateway _paymentGateWay;    
  8. private RegisterCourse _registerCourse;    
  9. private NotifyUser _notifyUser;    
  10. public RegistrationFacade()    
  11. {    
  12. _paymentGateWay = new PaymentGateway();    
  13. _registerCourse = new RegisterCourse();    
  14. _notifyUser = new NotifyUser();    
  15. }    
  16. public bool RegisterStudent(string courseCode, string studentName)    
  17.  {    
  18. //Step 1: Verify if there are available seats    
  19. if (!_registerCourse.CheckAvailability(courseCode))    
  20. return false;    
  21. //Step 2: Charge the student for tuition    
  22. if (!_paymentGateWay.ChargeStudent(studentName, _registerCourse.GetTuitionCost(courseCode)))    
  23. return false;    
  24. //Step 3: If everything's successful so far, notify the instructor of the new registration    
  25. return _notifyUser.Notify(studentName);   

3、客户端代码

  1. namespace FacadePattern    
  2. {    
  3. class Program    
  4.  {    
  5. static void Main(string[] args)    
  6. {    
  7. RegistrationFacade registrationFacade = new RegistrationFacade();    
  8. if (registrationFacade.RegisterStudent("DesignPatterns101", "Jane Doe"))    
  9. Console.WriteLine("Student Registration SUCCESSFUL!");    
  10. else   
  11. Console.WriteLine("Student Registration Unsuccessful");    
  12. }    
  13. }    
  14.  }   

五、总结(Summary)

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

外观模式可以解决层结构分离、降低系统耦合度和为新旧系统交互提供接口功能。

原文标题:Net设计模式实例之外观模式(Façade Pattern)

链接:http://www.cnblogs.com/ywqu/archive/2010/01/20/1652108.html

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...