C# 設計形式系列教程-外不雅形式。本站提示廣大學習愛好者:(C# 設計形式系列教程-外不雅形式)文章只能為提供參考,不一定能成為您想要的結果。以下是C# 設計形式系列教程-外不雅形式正文
1. 概述
為子體系中的一組接口供給一個分歧的界面,此形式界說了一個高層接口,這個接口使得這一子體系加倍輕易應用。
2. 形式中的腳色
2.1 外不雅類(Facade):外不雅類曉得哪些子體系類擔任處置要求,將客戶的要求署理給適當的子體系對象。
2.2 子體系類聚集(SubSystem Classes):子體系類聚集完成了子體系的功效,處置外不雅類對象指派的義務。
3. 形式解讀
3.1 外不雅形式的類圖

3.2 外不雅形式的代碼完成
/// <summary>
/// 子體系中的一個類
/// </summary>
public class SubSystemOne
{
public void MethodeOne()
{
Console.WriteLine("Sub System first method.");
}
}
/// <summary>
/// 子體系中一個類
/// </summary>
public class SubSystemTwo
{
public void MethodTwo()
{
Console.WriteLine("Sub System second method.");
}
}
/// <summary>
/// 子體系中一個類
/// </summary>
public class SubSystemThree
{
public void MethodThree()
{
Console.WriteLine("Sub System third method.");
}
}
/// <summary>
/// 子體系中一個類
/// </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的感化,客戶端可以基本不曉得子體系類的存在
Facade facade = new Facade();
facade.MethodA();
facade.MethodB();
Console.Read();
}
}
運轉成果

4. 形式總結
4.1 長處
4.1.1 Facade形式下降了客戶端對子體系應用的龐雜性。
4.1.2 外不雅形式松懈了客戶端與子體系的耦合關系,讓子體系外部的模塊能更輕易擴大和保護。
4.1.3 經由過程公道應用Facade,可以贊助我們更好的劃分拜訪的條理。
4.2 缺陷
過量的或許是不太公道的Facade也輕易讓人困惑,究竟是挪用Facade好呢,照樣直接挪用模塊好。
4.3 實用場景
4.3.1 須要將設計停止分層時斟酌Facade形式。
4.3.2 在開辟階段,子體系常常由於重構變得愈來愈龐雜,增長外不雅形式可以供給一個簡略的接口,削減它們之間的依附。
4.3.3 在保護一個遺留的年夜型體系時,可以這個體系曾經異常難以保護和擴大,可認為新體系開辟一個Facade類,來供給設計粗拙或高度龐雜的遺留代碼的比擬清楚簡略的接口,讓新體系與Facade對象交互,Facade與遺留代碼交互一切龐雜的任務。
5. 運用舉例:分層開辟中,對數據拜訪層我們增長DataAccess作為對外的接口來操作數據庫子體系。
5.1 完成類圖

5.2 完成代碼
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();
}
}
運轉成果

以上就是本文的全體內容,願望能給年夜家一個參考,也願望年夜家多多支撐。