您的位置:首页 > 其它

Net设计模式实例之外观模式(Façade Pattern)

2017-01-14 12:55 615 查看

一、外观模式简介(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");
    }
}
 

2、子系统类SubSystemTwo

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

3、子系统类SubSystemThree

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

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执行结束");
    }
}
 

5、客户端代码

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

3、程序运行结果



四.案例分析(Example)

1、场景

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

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

2、收取客户费用

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

如下图所示



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

PaymentGateway类:用户支付课程费用

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

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

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

2、代码

1、子系统类集合

1.   namespace FacadePattern   
2.   {   
3.       /// <summary>   
4.       /// Subsystem for making financial transactions   
5.       /// </summary>   
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.       /// <summary>   
17.       /// Subsystem for registration of courses   
18.       /// </summary>   
19.       public class RegisterCourse   
20.       {   
21.           public bool CheckAvailability(string courseCode)
  
22.           {   
23.               //Verify if the course is available..   
24.              Console.WriteLine(String.Format("Verifying availability of seats for the course : {0}", courseCode));   
25.               return true;   
26.           }   
27.     
28.           public int GetTuitionCost(string courseCode)
  
29.           {   
30.               //Get the cost of tuition   
31.               return 1000;   
32.           }   
33.       }   
34.     
35.       /// <summary>   
36.       /// Subsystem for Notifying users   
37.       /// </summary>   
38.       public class NotifyUser   
39.       {   
40.           public bool Notify(string studentName)
  
41.           {   
42.              //Get the name of the instructor based on Course Code   
43.               //Notify the instructor   
44.              Console.WriteLine("Notifying Instructor about new enrollment");   
45.               return true;   
46.           }   
47.       }   
48.   }

 

 

2、外观类Façade Class

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

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

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

 

版权
作者:灵动生活 郝宪玮

出处:http://www.cnblogs.com/ywqu

如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,

  加微信,与众多技术大咖交流: (请注明:cnblogs) 


本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: