成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

詳解.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

  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. /// <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. /// <summary>    
  16. /// Subsystem for registration of courses    
  17. /// </summary>    
  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.       /// <summary>    
  33.        /// Subsystem for Notifying users    
  34.       /// </summary>    
  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. /// <summary>    
  2. /// The Facade class that simplifies executing methods  
  3. in the subsystems and hides implementation for the client   
  4. /// </summary>    
  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

責任編輯:彭凡 來源: 博客園
相關推薦

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

2024-04-12 12:10:18

Python設計模式開發

2011-07-28 09:50:58

設計模式

2021-04-18 21:07:32

門面模式設計

2024-05-31 12:59:03

2024-07-31 08:12:33

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

設計模式到元

2009-07-10 16:14:29

MVC設計模式Swing
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91精品国产综合久久久久 | 国产成人精品久久二区二区91 | 国产精品视频网 | 久久大香 | 一区二区视频在线观看 | 日韩黄色免费 | 日本午夜一区二区三区 | 拍真实国产伦偷精品 | 国产精品国产精品国产专区不片 | 欧美视频成人 | 亚洲第一av网站 | 午夜影院在线观看 | 超碰伊人久久 | 久久精品小视频 | 亚洲国产精品久久 | 国产精品99久久久久久动医院 | 99久久精品国产毛片 | av看看| 欧美中文字幕一区二区 | 国产在线一区观看 | 毛片综合 | 亚洲成人一区 | 国产精品无码专区在线观看 | 日本三级电影在线观看视频 | 国产精品成人av | 久久国产高清视频 | 亚洲精品视频在线观看免费 | 91电影 | 亚洲一区二区三区观看 | 日韩视频中文字幕 | 国产成人一区二区三区 | 亚洲欧美日韩精品久久亚洲区 | 日韩在线观看中文字幕 | 亚洲 中文 欧美 日韩 在线观看 | 成人做爰www免费看视频网站 | 武道仙尊动漫在线观看 | 在线观看视频中文字幕 | 91精品国产一二三 | 久久曰视频 | 久久国产一区二区 | 草比网站|