国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 設計模式系列教程-外觀模式

2019-10-29 21:27:38
字體:
來源:轉載
供稿:網(wǎng)友
外觀模式松散了客戶端與子系統(tǒng)的耦合關系,讓子系統(tǒng)內部的模塊能更容易擴展和維護。
 

1. 概述

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

2. 模式中的角色

  2.1 外觀類(Facade):外觀類知道哪些子系統(tǒng)類負責處理請求,將客戶的請求代理給恰當?shù)淖酉到y(tǒng)對象。

  2.2 子系統(tǒng)類集合(SubSystem Classes):子系統(tǒng)類集合實現(xiàn)了子系統(tǒng)的功能,處理外觀類對象指派的任務。

3. 模式解讀

  3.1 外觀模式的類圖

C#,設計模式,外觀模式

  3.2 外觀模式的代碼實現(xiàn)

 /// <summary> /// 子系統(tǒng)中的一個類 /// </summary> public class SubSystemOne {  public void MethodeOne()  {   Console.WriteLine("Sub System first method.");  } } /// <summary> /// 子系統(tǒng)中一個類 /// </summary> public class SubSystemTwo {  public void MethodTwo()  {   Console.WriteLine("Sub System second method.");  } } /// <summary> /// 子系統(tǒng)中一個類 /// </summary> public class SubSystemThree {  public void MethodThree()  {   Console.WriteLine("Sub System third method.");  } } /// <summary> /// 子系統(tǒng)中一個類 /// </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();  } }

    運行結果

C#,設計模式,外觀模式

4. 模式總結

  4.1 優(yōu)點

    4.1.1 Facade模式降低了客戶端對子系統(tǒng)使用的復雜性。

    4.1.2 外觀模式松散了客戶端與子系統(tǒng)的耦合關系,讓子系統(tǒng)內部的模塊能更容易擴展和維護。

    4.1.3 通過合理使用Facade,可以幫助我們更好的劃分訪問的層次。

  4.2 缺點

    過多的或者是不太合理的Facade也容易讓人迷惑,到底是調用Facade好呢,還是直接調用模塊好。

  4.3 適用場景

    4.3.1 需要將設計進行分層時考慮Facade模式。

    4.3.2 在開發(fā)階段,子系統(tǒng)往往因為重構變得越來越復雜,增加外觀模式可以提供一個簡單的接口,減少它們之間的依賴。

    4.3.3 在維護一個遺留的大型系統(tǒng)時,可以這個系統(tǒng)已經非常難以維護和擴展,可以為新系統(tǒng)開發(fā)一個Facade類,來提供設計粗糙或高度復雜的遺留代碼的比較清晰簡單的接口,讓新系統(tǒng)與Facade對象交互,F(xiàn)acade與遺留代碼交互所有復雜的工作。

5. 應用舉例:分層開發(fā)中,對數(shù)據(jù)訪問層我們增加DataAccess作為對外的接口來操作數(shù)據(jù)庫子系統(tǒng)。

  5.1 實現(xiàn)類圖

C#,設計模式,外觀模式

  5.2 實現(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為客戶端提供一個簡單的接口 /// </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();  } }

  運行結果

C#,設計模式,外觀模式

以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支持VEVB武林網(wǎng)。



注:相關教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 江西省| 辽源市| 泸州市| 报价| 洛隆县| 永胜县| 平武县| 西盟| 商丘市| 望城县| 浦江县| 南雄市| 唐海县| 鄂托克前旗| 东乌珠穆沁旗| 鹰潭市| 鄂伦春自治旗| 土默特右旗| 甘肃省| 新乐市| 静宁县| 长垣县| 宜兰市| 奉新县| 综艺| 阿巴嘎旗| 民丰县| 新乡县| 合水县| 榆社县| 尚义县| 敦煌市| 鄄城县| 永和县| 乌兰县| 定陶县| 昌乐县| 霍城县| 平度市| 庆安县| 斗六市|