自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解.NET設(shè)計(jì)模式實(shí)例中的外觀模式

開(kāi)發(fā) 后端
在這里我們將討論的是.NET設(shè)計(jì)模式實(shí)例中的外觀模式,這也是我們經(jīng)常需要考慮的問(wèn)題。

本文將介紹的是外觀模式,也就是.NET設(shè)計(jì)模式中的一種。在這里希望通過(guò)本文,能讓大家對(duì).NET設(shè)計(jì)模式有進(jìn)一步的了解,同時(shí)也提供了相應(yīng)代碼實(shí)例供大家參考。

#T#

一、外觀模式簡(jiǎn)介(Brief Introduction)

外觀模式,為子系統(tǒng)的一組接口提供一個(gè)統(tǒng)一的界面,此模式定義了一個(gè)高層接口,這一個(gè)高層接口使的子系統(tǒng)更加容易使用。

二、解決的問(wèn)題(What To Solve)

1、分離不同的兩個(gè)層

典型的分層例子是Net三層架構(gòu),界面層與業(yè)務(wù)邏輯層分離,業(yè)務(wù)邏輯層與數(shù)據(jù)訪問(wèn)層分類。這樣可以為子系統(tǒng)提供統(tǒng)一的界面和接口,降低了系統(tǒng)的耦合性。

2、減少依賴

隨著功能增加及程序的重構(gòu),系統(tǒng)會(huì)變得越來(lái)越復(fù)雜,這時(shí)增加一個(gè)外觀可以提供一個(gè)簡(jiǎn)單的接口,減少他們之間的依賴。

3、為新舊系統(tǒng)交互提供接口

有的時(shí)候,新系統(tǒng)需要舊系統(tǒng)的核心功能,而這個(gè)舊的系統(tǒng)已經(jīng)很難維護(hù)和擴(kuò)展,可以給新系統(tǒng)增加一個(gè)Façade類,是的新系統(tǒng)與Façade類交互,F(xiàn)açade類與舊系統(tǒng)交互素有復(fù)雜的工作。

三、外觀模式分析(Analysis)

1、外觀模式結(jié)構(gòu)

2、源代碼

1、子系統(tǒng)類SubSystemOne

  1. public class SubSystemOne  
  2. {  
  3.     public void MethodOne()  
  4.     {  
  5.         Console.WriteLine("執(zhí)行子系統(tǒng)One中的方法One");  
  6.     }  

2、子系統(tǒng)類SubSystemTwo

  1. public class SubSystemTwo  
  2. {  
  3.     public void MethodTwo()  
  4.     {  
  5.         Console.WriteLine("執(zhí)行子系統(tǒng)Two中的方法Two");  
  6.     }  

3、子系統(tǒng)類SubSystemThree

  1. public class SubSystemThree  
  2. {  
  3.     public void MethodThree()  
  4.     {  
  5.         Console.WriteLine("執(zhí)行子系統(tǒng)Three中的方法Three");  
  6.     }  

4、Facade 外觀類,為子系統(tǒng)類集合提供更高層次的接口和一致的界面

  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("開(kāi)始執(zhí)行外觀模式中的方法A");  
  15.         one.MethodOne();  
  16.         two.MethodTwo();  
  17.         Console.WriteLine("外觀模式中的方法A執(zhí)行結(jié)束");  
  18.         Console.WriteLine("---------------------------");  
  19.     }  
  20.     public void MethodB()  
  21.    {  
  22.         Console.WriteLine("開(kāi)始執(zhí)行外觀模式中的方法B");  
  23.         two.MethodTwo();  
  24.         three.MethodThree();  
  25.         Console.WriteLine("外觀模式中的方法B執(zhí)行結(jié)束");  
  26.     }  

5、客戶端代碼

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

3、程序運(yùn)行結(jié)果

運(yùn)行結(jié)果

四.案例分析(Example)

1、場(chǎng)景

假設(shè)遠(yuǎn)程網(wǎng)絡(luò)教育系統(tǒng)-用戶注冊(cè)模塊包括功能有

1、驗(yàn)證課程是否已經(jīng)滿人

2、收取客戶費(fèi)用

3、通知用戶課程選擇成功

如下圖所示

通知用戶選擇成功

子系統(tǒng)類集合包括:PaymentGateway類、RegisterCourse類、NotifyUser類

PaymentGateway類:用戶支付課程費(fèi)用

RegisterCourse類:驗(yàn)證所選課程是否已經(jīng)滿人以及計(jì)算課程的費(fèi)用

NotifyUser類:" 用戶選擇課程成功與否"通知用戶

RegistrationFacade類:外觀類,提供一個(gè)統(tǒng)一的界面和接口,完成課程校驗(yàn)、網(wǎng)上支付、通知用戶功能

2、代碼

1、子系統(tǒng)類集合

  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.  }   

五、總結(jié)(Summary)

外觀模式,為子系統(tǒng)的一組接口提供一個(gè)統(tǒng)一的界面,此模式定義了一個(gè)高層接口,這一個(gè)高層接口使的子系統(tǒng)更加容易使用。

外觀模式可以解決層結(jié)構(gòu)分離、降低系統(tǒng)耦合度和為新舊系統(tǒng)交互提供接口功能。

原文標(biāo)題:Net設(shè)計(jì)模式實(shí)例之外觀模式(Façade Pattern)

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

責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2020-10-23 09:40:26

設(shè)計(jì)模式

2022-02-15 22:45:00

前端設(shè)計(jì)模式

2021-03-18 15:33:22

設(shè)計(jì)模式外觀

2023-07-03 07:39:43

Spring框架設(shè)計(jì)模式

2021-06-29 08:54:23

設(shè)計(jì)模式代理模式遠(yuǎn)程代理

2023-09-22 11:58:49

2012-08-30 09:07:33

設(shè)計(jì)模式

2012-04-05 11:35:07

.NET

2021-04-18 21:07:32

門面模式設(shè)計(jì)

2024-04-12 12:10:18

Python設(shè)計(jì)模式開(kāi)發(fā)

2024-07-31 08:12:33

2011-07-28 09:50:58

設(shè)計(jì)模式

2024-05-31 12:59:03

2009-08-18 11:03:31

Observer設(shè)計(jì)模

2011-09-14 10:29:23

Android UI設(shè)

2023-07-13 09:28:29

設(shè)計(jì)模式.NET

2024-06-06 08:32:52

.NET框架代碼

2021-04-14 09:02:22

模式 設(shè)計(jì)建造者

2021-04-19 21:25:48

設(shè)計(jì)模式到元

2017-02-17 10:07:02

AndroidMVP模式實(shí)例
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)