1. 概述
為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,此模式定義了一個(gè)高層接口,這個(gè)接口使得這一子系統(tǒng)更加容易使用。
2. 模式中的角色
2.1 外觀類(facade):外觀類知道哪些子系統(tǒng)類負(fù)責(zé)處理請(qǐng)求,將客戶的請(qǐng)求代理給恰當(dāng)?shù)淖酉到y(tǒng)對(duì)象。
2.2 子系統(tǒng)類集合(subsystem classes):子系統(tǒng)類集合實(shí)現(xiàn)了子系統(tǒng)的功能,處理外觀類對(duì)象指派的任務(wù)。
3. 模式解讀
3.1 外觀模式的類圖
3.2 外觀模式的代碼實(shí)現(xiàn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | /// <summary> /// 子系統(tǒng)中的一個(gè)類 /// </summary> public class subsystemone { public void methodeone() { console.writeline( "sub system first method." ); } } /// <summary> /// 子系統(tǒng)中一個(gè)類 /// </summary> public class subsystemtwo { public void methodtwo() { console.writeline( "sub system second method." ); } } /// <summary> /// 子系統(tǒng)中一個(gè)類 /// </summary> public class subsystemthree { public void methodthree() { console.writeline( "sub system third method." ); } } /// <summary> /// 子系統(tǒng)中一個(gè)類 /// </summary> public class subsystemfour { public void methodfour() { console.writeline( "sub system fourth method." ); } } /// <summary> /// 外觀類 /// </summary> public class facade { private subsystemone one; private subsystemtwo two; private subsystemthree three; private subsystemfour four; public facade() { one = new subsystemone(); two = new subsystemtwo(); three = new subsystemthree(); four = new subsystemfour(); } public void methoda() { console.writeline( "\nmethod group a----" ); one.methodeone(); two.methodtwo(); four.methodfour(); } public void methodb() { console.writeline( "\nmethod group b----" ); two.methodtwo(); three.methodthree(); } } |
3.3 客戶端代碼
1 2 3 4 5 6 7 8 9 10 11 12 | class program { static void main( string [] args) { // 由于facade的作用,客戶端可以根本不知道子系統(tǒng)類的存在 facade facade = new facade(); facade.methoda(); facade.methodb(); console.read(); } } |
運(yùn)行結(jié)果
4. 模式總結(jié)
4.1 優(yōu)點(diǎn)
4.1.1 facade模式降低了客戶端對(duì)子系統(tǒng)使用的復(fù)雜性。
4.1.2 外觀模式松散了客戶端與子系統(tǒng)的耦合關(guān)系,讓子系統(tǒng)內(nèi)部的模塊能更容易擴(kuò)展和維護(hù)。
4.1.3 通過合理使用facade,可以幫助我們更好的劃分訪問的層次。
4.2 缺點(diǎn)
過多的或者是不太合理的facade也容易讓人迷惑,到底是調(diào)用facade好呢,還是直接調(diào)用模塊好。
4.3 適用場景
4.3.1 需要將設(shè)計(jì)進(jìn)行分層時(shí)考慮facade模式。
4.3.2 在開發(fā)階段,子系統(tǒng)往往因?yàn)橹貥?gòu)變得越來越復(fù)雜,增加外觀模式可以提供一個(gè)簡單的接口,減少它們之間的依賴。
4.3.3 在維護(hù)一個(gè)遺留的大型系統(tǒng)時(shí),可以這個(gè)系統(tǒng)已經(jīng)非常難以維護(hù)和擴(kuò)展,可以為新系統(tǒng)開發(fā)一個(gè)facade類,來提供設(shè)計(jì)粗糙或高度復(fù)雜的遺留代碼的比較清晰簡單的接口,讓新系統(tǒng)與facade對(duì)象交互,facade與遺留代碼交互所有復(fù)雜的工作。
5. 應(yīng)用舉例:分層開發(fā)中,對(duì)數(shù)據(jù)訪問層我們?cè)黾觗ataaccess作為對(duì)外的接口來操作數(shù)據(jù)庫子系統(tǒng)。
5.1 實(shí)現(xiàn)類圖
5.2 實(shí)現(xiàn)代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | public class employee { public string name { get ; set ; } public int age { get ; set ; } public salary salary { get ; set ; } } public class salary { public datetime from { get ; set ; } public datetime to { get ; set ; } public decimal amount { get ; set ; } } public class employeedataaccess { public void saveemployee(employee employee) { console.writeline( "save employee to database." ); } public void deleteemployee(employee employee) { console.writeline( "remode employee from database." ); } } public class salarydataaccess { public void savesalary(salary salary) { console.writeline( "save salary to database." ); } public void deletesalary(salary salary) { console.writeline( "remove salary from database." ); } } /// <summary> /// dataaccess為客戶端提供一個(gè)簡單的接口 /// </summary> public class dataaccess { private employeedataaccess employeedataaccess = new employeedataaccess(); private salarydataaccess salarydataaccess = new salarydataaccess(); public void saveemployee(employee employee) { // 先保存員工基本信息 employeedataaccess.saveemployee(employee); // 保存員工薪水信息 salarydataaccess.savesalary(employee.salary); } public void removeemployee(employee employee) { // 先刪除員工薪水信息 salarydataaccess.deletesalary(employee.salary); // 刪除員工基本信息 employeedataaccess.deleteemployee(employee); } } |
5.3 客戶端代碼
1 2 3 4 5 6 7 8 9 10 11 12 13 | class program { static void main( string [] args) { dataaccess.dataaccess dataaccess = new dataaccess.dataaccess(); dataaccess.employee employee = new dataaccess.employee() { salary = new dataaccess.salary(), name = "wang kevin" , age = 22 }; dataaccess.saveemployee(employee); dataaccess.removeemployee(employee); console.read(); } } |
運(yùn)行結(jié)果
以上就是本文的全部內(nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。