詳解.NET設(shè)計(jì)模式實(shí)例中的外觀模式
本文將介紹的是外觀模式,也就是.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
- public class SubSystemOne
- {
- public void MethodOne()
- {
- Console.WriteLine("執(zhí)行子系統(tǒng)One中的方法One");
- }
- }
2、子系統(tǒng)類SubSystemTwo
- public class SubSystemTwo
- {
- public void MethodTwo()
- {
- Console.WriteLine("執(zhí)行子系統(tǒng)Two中的方法Two");
- }
- }
3、子系統(tǒng)類SubSystemThree
- public class SubSystemThree
- {
- public void MethodThree()
- {
- Console.WriteLine("執(zhí)行子系統(tǒng)Three中的方法Three");
- }
- }
4、Facade 外觀類,為子系統(tǒng)類集合提供更高層次的接口和一致的界面
- 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("開(kāi)始執(zhí)行外觀模式中的方法A");
- one.MethodOne();
- two.MethodTwo();
- Console.WriteLine("外觀模式中的方法A執(zhí)行結(jié)束");
- Console.WriteLine("---------------------------");
- }
- public void MethodB()
- {
- Console.WriteLine("開(kāi)始執(zhí)行外觀模式中的方法B");
- two.MethodTwo();
- three.MethodThree();
- Console.WriteLine("外觀模式中的方法B執(zhí)行結(jié)束");
- }
- }
5、客戶端代碼
- static void Main(string[] args)
- {
- Facade facade = new Facade();
- facade.MethodA();
- facade.MethodB();
- Console.Read();
- }
3、程序運(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)類集合
- namespace FacadePattern
- {
- /// <summary>
- /// Subsystem for making financial transactions
- /// </summary>
- public class PaymentGateway
- {
- public bool ChargeStudent(string studentName, int costTuition)
- {
- //Charge the student
- Console.WriteLine(String.Format("Charging student {0} for ${1}", studentName, costTuition.ToString()));
- return true;
- }
- }
- /// <summary>
- /// Subsystem for registration of courses
- /// </summary>
- public class RegisterCourse
- {
- public bool CheckAvailability(string courseCode)
- {
- //Verify if the course is available..
- Console.WriteLine(String.Format("Verifying availability of seats for the course : {0}", courseCode));
- return true;
- }
- public int GetTuitionCost(string courseCode)
- {
- //Get the cost of tuition
- return 1000;
- }
- }
- /// <summary>
- /// Subsystem for Notifying users
- /// </summary>
- public class NotifyUser
- {
- public bool Notify(string studentName)
- {
- //Get the name of the instructor based on Course Code
- //Notify the instructor
- Console.WriteLine("Notifying Instructor about new enrollment");
- return true;
- }
- }
- }
2、外觀類Façade Class
- /// <summary>
- /// The Facade class that simplifies executing methods
- in the subsystems and hides implementation for the client
- /// </summary>
- public class RegistrationFacade
- {
- private PaymentGateway _paymentGateWay;
- private RegisterCourse _registerCourse;
- private NotifyUser _notifyUser;
- public RegistrationFacade()
- {
- _paymentGateWay = new PaymentGateway();
- _registerCourse = new RegisterCourse();
- _notifyUser = new NotifyUser();
- }
- public bool RegisterStudent(string courseCode, string studentName)
- {
- //Step 1: Verify if there are available seats
- if (!_registerCourse.CheckAvailability(courseCode))
- return false;
- //Step 2: Charge the student for tuition
- if (!_paymentGateWay.ChargeStudent(studentName, _registerCourse.GetTuitionCost(courseCode)))
- return false;
- //Step 3: If everything's successful so far, notify the instructor of the new registration
- return _notifyUser.Notify(studentName);
3、客戶端代碼
- namespace FacadePattern
- {
- class Program
- {
- static void Main(string[] args)
- {
- RegistrationFacade registrationFacade = new RegistrationFacade();
- if (registrationFacade.RegisterStudent("DesignPatterns101", "Jane Doe"))
- Console.WriteLine("Student Registration SUCCESSFUL!");
- else
- Console.WriteLine("Student Registration Unsuccessful");
- }
- }
- }
五、總結(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