日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解.NET設(shè)計(jì)模式實(shí)例中的外觀模式

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

10年的沿河網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整沿河建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“沿河網(wǎng)站設(shè)計(jì)”,“沿河網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

#T#

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

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

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

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

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

2、減少依賴

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

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("開始執(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("開始執(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é)果

四.案例分析(Example)

1、場景

假設(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. ///     
  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.  }   

五、總結(jié)(Summary)

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

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


網(wǎng)頁標(biāo)題:詳解.NET設(shè)計(jì)模式實(shí)例中的外觀模式
文章地址:http://m.5511xx.com/article/cdjhpip.html