C# 設(shè)計(jì)模式系列教程-外觀模式
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)
/// <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 客戶端代碼
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 適用場(chǎng)景
4.3.1 需要將設(shè)計(jì)進(jìn)行分層時(shí)考慮Facade模式。
4.3.2 在開發(fā)階段,子系統(tǒng)往往因?yàn)橹貥?gòu)變得越來越復(fù)雜,增加外觀模式可以提供一個(gè)簡(jiǎn)單的接口,減少它們之間的依賴。
4.3.3 在維護(hù)一個(gè)遺留的大型系統(tǒng)時(shí),可以這個(gè)系統(tǒng)已經(jīng)非常難以維護(hù)和擴(kuò)展,可以為新系統(tǒng)開發(fā)一個(gè)Facade類,來提供設(shè)計(jì)粗糙或高度復(fù)雜的遺留代碼的比較清晰簡(jiǎn)單的接口,讓新系統(tǒng)與Facade對(duì)象交互,F(xiàn)acade與遺留代碼交互所有復(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)代碼
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è)簡(jiǎn)單的接口
/// </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 客戶端代碼
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é)果

以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 解析C#設(shè)計(jì)模式編程中外觀模式Facade Pattern的應(yīng)用
- C#設(shè)計(jì)模式之外觀模式介紹
- C#設(shè)計(jì)模式之單例模式實(shí)例講解
- c#設(shè)計(jì)模式 適配器模式詳細(xì)介紹
- C#設(shè)計(jì)模式之觀察者模式實(shí)例講解
- 淺談C#設(shè)計(jì)模式之代理模式
- 詳解C#的設(shè)計(jì)模式編程之抽象工廠模式的應(yīng)用
- C#中利用代理實(shí)現(xiàn)觀察者設(shè)計(jì)模式詳解
- C# 設(shè)計(jì)模式系列教程-策略模式
- 解析C#設(shè)計(jì)模式編程中的裝飾者模式
- C#設(shè)計(jì)模式之Facade外觀模式解決天河城購(gòu)物問題示例
相關(guān)文章
使用xmltextreader對(duì)象讀取xml文檔示例
這篇文章主要介紹了使用xmltextreader對(duì)象讀取xml文檔的示例,需要的朋友可以參考下2014-02-02
C# TreeView從數(shù)據(jù)庫綁定數(shù)據(jù)的示例
這篇文章主要介紹了C# TreeView從數(shù)據(jù)庫綁定數(shù)據(jù)的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C# 啟動(dòng) SQL Server 服務(wù)的實(shí)例
下面小編就為大家分享一篇C# 啟動(dòng) SQL Server 服務(wù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12
C#實(shí)現(xiàn)將浮點(diǎn)數(shù)表示的貨幣數(shù)量以漢字大寫形式輸出的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將浮點(diǎn)數(shù)表示的貨幣數(shù)量以漢字大寫形式輸出的方法,涉及C#針對(duì)浮點(diǎn)數(shù)的遍歷與字符替換操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
C#實(shí)現(xiàn)WinForm全屏置頂?shù)氖纠a
我們?cè)谶\(yùn)行一些?Windows?應(yīng)用程序的時(shí)候,需要將其運(yùn)行在窗體置頂?shù)哪J?并且進(jìn)入全屏狀態(tài),本文將介紹如何使用?C#?來實(shí)現(xiàn)?WinForm?的全屏置頂?shù)幕竟δ?感興趣的可以了解下2024-12-12
利用微軟com組件mstscax.dll實(shí)現(xiàn)window7遠(yuǎn)程桌面功能
利用微軟提供的com組件mstscax.dll實(shí)現(xiàn)類似window遠(yuǎn)程桌面功能,大家參考使用吧2013-12-12
WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法,涉及WinForm計(jì)時(shí)器及進(jìn)程操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09

