详解.NET设计模式实例中的外观模式

开发 后端
在这里我们将讨论的是.NET设计模式实例中的外观模式,这也是我们经常需要考虑的问题。

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

#T#

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

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

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

1、分离不同的两个层

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

2、减少依赖

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

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

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

三、外观模式分析(Analysis)

1、外观模式结构

2、源代码

1、子系统类SubSystemOne

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

2、子系统类SubSystemTwo

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

3、子系统类SubSystemThree

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

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

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

public class Facade  
{  
    SubSystemOne one;  
    SubSystemTwo two;  
    SubSystemThree three;  
    public Facade()  
    {  
       one = new SubSystemOne();  
        two = new SubSystemTwo();  
        three = new SubSystemThree();  
    }  
    public void MethodA()  
    {  
        Console.WriteLine("开始执行外观模式中的方法A");  
        one.MethodOne();  
        two.MethodTwo();  
        Console.WriteLine("外观模式中的方法A执行结束");  
        Console.WriteLine("---------------------------");  
    }  
    public void MethodB()  
   {  
        Console.WriteLine("开始执行外观模式中的方法B");  
        two.MethodTwo();  
        three.MethodThree();  
        Console.WriteLine("外观模式中的方法B执行结束");  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

5、客户端代码

static void Main(string[] args)  
{  
    Facade facade = new Facade();  
    facade.MethodA();  
    facade.MethodB();  
    Console.Read();  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

3、程序运行结果

运行结果

四.案例分析(Example)

1、场景

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

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

2、收取客户费用

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

如下图所示

通知用户选择成功

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

PaymentGateway类:用户支付课程费用

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

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

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

2、代码

1、子系统类集合

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

2、外观类Façade Class

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

3、客户端代码

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

五、总结(Summary)

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

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

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

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

责任编辑:彭凡 来源: 博客园
相关推荐

2020-10-23 09:40:26

设计模式

2022-02-15 22:45:00

前端设计模式

2021-03-18 15:33:22

设计模式外观

2023-07-03 07:39:43

Spring框架设计模式

2021-06-29 08:54:23

设计模式代理模式远程代理

2023-09-22 11:58:49

2012-08-30 09:07:33

设计模式

2012-04-05 11:35:07

.NET

2021-04-18 21:07:32

门面模式设计

2024-04-12 12:10:18

Python设计模式开发

2024-07-31 08:12:33

2011-07-28 09:50:58

设计模式

2024-05-31 12:59:03

2009-08-18 11:03:31

Observer设计模

2011-09-14 10:29:23

Android UI设

2023-07-13 09:28:29

设计模式.NET

2024-06-06 08:32:52

.NET框架代码

2021-04-14 09:02:22

模式 设计建造者

2021-04-19 21:25:48

设计模式到元

2017-02-17 10:07:02

AndroidMVP模式实例
点赞
收藏

51CTO技术栈公众号