针对美国企业为系统建模
为了拓展国际市场,我们要把该系统移植给美国公司使用。 美国企业的工资计算同样是: 员工的工资 = 基本工资 + 奖金 - 个人所得税。
但是他们的奖金和个人所得税的计算规则不同于中国企业:
美国企业奖金和个人所得税的计算规则是:
奖金 = 基本工资 * 15 %
个人所得税 = (基本工资 * 5% + 奖金 * 25%)
根据前面为中国企业建模经验,我们仅仅将ChineseTax、ChineseBonus修改为AmericanTax、AmericanBonus。 修改后的模型如下:
则业务规则Service类的代码如下:
1using System; 2 3namespace AmericanSalary 4{ 5 /**//// <summary> 6 /// 公用的常量 7 /// </summary> 8 public class Constant 9 { 10 public static double BASE_SALARY = 4000; 11 } 12} 13 1using System; 2 3namespace AmericanSalary 4{ 5 /**//// <summary> 6 /// 计算美国个人奖金 7 /// </summary> 8 public class AmericanBonus 9 { 10 public double Calculate() 11 { 12 return Constant.BASE_SALARY * 0.1; 13 } 14 } 15} 16 1using System; 2 3namespace AmericanSalary 4{ 5 /**//// <summary> 6 /// 计算美国个人所得税 7 /// </summary> 8 public class AmericanTax 9 { 10 public double Calculate() 11 { 12 return (Constant.BASE_SALARY + (Constant.BASE_SALARY * 0.1)) * 0.4; 13 } 14 } 15} 16 |
1 2using System; 3 4namespace AmericanSalary 5{ 6 /**//// <summary> 7 /// 客户端程序调用 8 /// </summary> 9 public class Calculator 10 { 11 public static void Main(string[] args) 12 { 13 AmericanBonus bonus = new AmericanBonus(); 14 double bonusValue = bonus.Calculate(); 15 16 AmericanTax tax = new AmericanTax(); 17 double taxValue = tax.Calculate(); 18 19 double salary = 4000 + bonusValue - taxValue; 20 21 Console.WriteLine("American Salary is:" + salary); 22 Console.ReadLine(); 23 } 24 } 25} 26 |
通过保留中国企业和美国企业的业务规则模型,如果该系统在美国企业和中国企业之间切换时,我们仅仅需要修改Caculator类即可。
让移植工作更简单
前面系统的整合问题在于:当系统在客户在美国和中国企业间切换时仍然需要修改Caculator代码。
一个维护性良好的系统应该遵循“开闭原则”。即:封闭对原来代码的修改,开放对原来代码的扩展(如类的继承,接口的实现)
我们发现不论是中国企业还是美国企业,他们的业务运规则都采用同样的计算接口。 于是很自然地想到建立两个业务接口类Tax,Bonus,然后让AmericanTax、AmericanBonus和ChineseTax、ChineseBonus分别实现这两个接口, 据此修正后的模型如下:
此时客户端代码如下:
1 2using System; 3 4namespace InterfaceSalary 5{ 6 /**//// <summary> 7 /// 客户端程序调用 8 /// </summary> 9 public class Calculator 10 { 11 public static void Main(string[] args) 12 { 13 Bonus bonus = new ChineseBonus(); 14 double bonusValue = bonus.Calculate(); 15 16 Tax tax = new ChineseTax(); 17 double taxValue = tax.Calculate(); 18 19 double salary = 4000 + bonusValue - taxValue; 20 21 Console.WriteLine("Chinaese Salary is:" + salary); 22 Console.ReadLine(); 23 } 24 } 25} 26 |
1using System; 2 3namespace FactorySalary 4{ 5 /**//// <summary> 6 /// Factory类 7 /// </summary> 8 public class Factory 9 { 10 public Tax CreateTax() 11 { 12 return new ChineseTax(); 13 } 14 15 public Bonus CreateBonus() 16 { 17 return new ChineseBonus(); 18 } 19 } 20} 21 |
1 2using System; 3 4namespace FactorySalary 5{ 6 /**//// <summary> 7 /// 客户端程序调用 8 /// </summary> 9 public class Calculator 10 { 11 public static void Main(string[] args) 12 { 13 Bonus bonus = new Factory().CreateBonus(); 14 double bonusValue = bonus.Calculate(); 15 16 Tax tax = new Factory().CreateTax(); 17 double taxValue = tax.Calculate(); 18 19 double salary = 4000 + bonusValue - taxValue; 20 21 Console.WriteLine("Chinaese Salary is:" + salary); 22 Console.ReadLine(); 23 } 24 } 25} 26 |
1using System; 2 3namespace FactorySalary 4{ 5 /**//// <summary> 6 /// Factory类 7 /// </summary> 8 public class Factory 9 { 10 public Tax CreateTax() 11 { 12 return new AmericanTax(); 13 } 14 15 public Bonus CreateBonus() 16 { 17 return new AmericanBonus(); 18 } 19 } 20} 21 |
抽象工厂类的代码如下:
1using System; 2using System.Reflection; 3 4namespace AbstractFactory 5{ 6 /**//// <summary> 7 /// AbstractFactory类 8 /// </summary> 9 public abstract class AbstractFactory 10 { 11 public static AbstractFactory GetInstance() 12 { 13 string factoryName = Constant.STR_FACTORYNAME.ToString(); 14 15 AbstractFactory instance; 16 17 if(factoryName == "ChineseFactory") 18 instance = new ChineseFactory(); 19 else if(factoryName == "AmericanFactory") 20 instance = new AmericanFactory(); 21 else 22 instance = null; 23 24 return instance; 25 } 26 27 public abstract Tax CreateTax(); 28 29 public abstract Bonus CreateBonus(); 30 } 31} |
1<?xml version="1.0" encoding="utf-8" ?> 2<configuration> 3 <appSettings> 4 <add key="factoryName" value="AmericanFactory"></add> 5 </appSettings> 6</configuration> 7 |
1using System; 2using System.Reflection; 3 4namespace AbstractFactory 5{ 6 /**//// <summary> 7 /// AbstractFactory类 8 /// </summary> 9 public abstract class AbstractFactory 10 { 11 public static AbstractFactory GetInstance() 12 { 13 string factoryName = Constant.STR_FACTORYNAME.ToString(); 14 15 AbstractFactory instance; 16 17 if(factoryName != "") 18 instance = (AbstractFactory)Assembly.Load(factoryName).CreateInstance(factoryName); 19 else 20 instance = null; 21 22 return instance; 23 } 24 25 public abstract Tax CreateTax(); 26 27 public abstract Bonus CreateBonus(); 28 } 29} 30 |