天天看點

Net設計模式執行個體之外觀模式(Façade Pattern)

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

       典型的分層例子是Net三層架構,界面層與業務邏輯層分離,業務邏輯層與資料通路層分類。這樣可以為子系統提供統一的界面和接口,降低了系統的耦合性。

       随着功能增加及程式的重構,系統會變得越來越複雜,這時增加一個外觀可以提供一個簡單的接口,減少他們之間的依賴。

       有的時候,新系統需要舊系統的核心功能,而這個舊的系統已經很難維護和擴充,可以給新系統增加一個Façade類,是的新系統與Façade類互動,Façade類與舊系統互動素有複雜的工作。

Net設計模式執行個體之外觀模式(Façade Pattern)

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");

        three.MethodThree();

        Console.WriteLine("外觀模式中的方法B執行結束");

5、用戶端代碼

static void Main(string[] args)

    Facade facade = new Facade();

    facade.MethodA();

    facade.MethodB();

    Console.Read();

Net設計模式執行個體之外觀模式(Façade Pattern)

假設遠端網絡教育系統-使用者注冊子產品包括功能有

1、驗證課程是否已經滿人

2、收取客戶費用

3、通知使用者課程選擇成功

如下圖所示

Net設計模式執行個體之外觀模式(Façade Pattern)

子系統類集合包括:PaymentGateway類、RegisterCourse類、NotifyUser類

PaymentGateway類:使用者支付課程費用

RegisterCourse類:驗證所選課程是否已經滿人以及計算課程的費用

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

RegistrationFacade類:外觀類,提供一個統一的界面和接口,完成課程校驗、網上支付、通知使用者功能

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、用戶端代碼

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

外觀模式可以解決層結構分離、降低系統耦合度和為新舊系統互動提供接口功能。

版權

作者:靈動生活 郝憲玮

如果你認為此文章有用,請點選底端的【推薦】讓其他人也了解此文章,

Net設計模式執行個體之外觀模式(Façade Pattern)

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。

繼續閱讀