/// <summary> /// 适配器模式 亦称: 封装器模式、Wrapper、Adapter Adapter Pattern /// </summary> public class Employee { /// <summary> /// /// </summary> public int Id { get; set; } /// <summary> /// /// </summary> public string Name { get; set; } = string.Empty; /// <summary> /// /// </summary> public decimal Salary { get; set; } } /// <summary> /// 适配器模式 亦称: 封装器模式、Wrapper、Adapter Adapter Pattern /// </summary> public class HRSystem { /// <summary> /// /// </summary> /// <returns></returns> public string[,] GetEmployeesInfo() => new string[4, 3] { { "1", "GeovinDu", "10001" }, { "2", "geovindu", "10002" }, { "3", "涂聚文", "10003" }, { "4", "geovindu", "10004" }, }; } /// <summary> /// 适配器模式 亦称: 封装器模式、Wrapper、Adapter Adapter Pattern /// </summary> public interface ISalaryProcessor { /// <summary> /// /// </summary> /// <param name="rawEmployees"></param> void ProcessSalaries(string[,] rawEmployees); } /// <summary> /// 适配器模式 亦称: 封装器模式、Wrapper、Adapter Adapter Pattern /// </summary> public class HRSystemAdapter : ISalaryProcessor { /// <summary> /// /// </summary> private readonly ThirdPartyBillingSystem _thirdPartyBillingSystem; /// <summary> /// /// </summary> public HRSystemAdapter() { _thirdPartyBillingSystem = new ThirdPartyBillingSystem(); } /// <summary> /// /// </summary> /// <param name="rawEmployees"></param> public void ProcessSalaries(string[,] rawEmployees) { var employeesForProcessing = PrepareEmployeesForSalaryProcessing(rawEmployees); _thirdPartyBillingSystem.ProcessSalary(employeesForProcessing); } /// <summary> /// /// </summary> /// <param name="rawEmployees"></param> /// <returns></returns> private static List<Employee> PrepareEmployeesForSalaryProcessing(string[,] rawEmployees) { var employeesForProcessing = new List<Employee>(); for (var i = 0; i < rawEmployees.GetLength(0); i++) { var id = string.Empty; var name = string.Empty; var salary = string.Empty; for (var j = 0; j < rawEmployees.GetLength(1); j++) { switch (j) { case 0: id = rawEmployees[i, j]; break; case 1: name = rawEmployees[i, j]; break; default: salary = rawEmployees[i, j]; break; } } var employee = new Employee { Id = Convert.ToInt32(id, CultureInfo.InvariantCulture), Name = name, Salary = Convert.ToDecimal(salary, CultureInfo.InvariantCulture), }; employeesForProcessing.Add(employee); } Console.WriteLine("适配器将雇员数组转换为雇员列表..."); return employeesForProcessing; } } /// <summary> /// 适配器模式 亦称: 封装器模式、Wrapper、Adapter Adapter Pattern /// </summary> public class ThirdPartyBillingSystem { /// <summary> /// /// </summary> /// <param name="employees"></param> public void ProcessSalary(List<Employee> employees) { foreach (var employee in employees) { Console.WriteLine($"工号: {employee.Salary} 工资记入 {employee.Name}' 一个账号."); } } }
调用:
Geovin.Du.DuAdapter.BillingSystem.BillingSystemExecutor.Execute();
输出:
适配器模式 Adapter Pattern: Billing system Demo -------------------------------------------------- 适配器将雇员数组转换为雇员列表... 工号: 10001 工资记入 GeovinDu' 一个账号. 工号: 10002 工资记入 geovindu' 一个账号. 工号: 10003 工资记入 涂聚文' 一个账号. 工号: 10004 工资记入 geovindu' 一个账号.
标签:donet,string,Pattern,Adapter,rawEmployees,var,适配器,public From: https://www.cnblogs.com/geovindu/p/16885186.html