public interface ISalary { void CommandSalary(); }
public abstract class ShenZhenSalary : ISalary { public virtual void CommandSalary() { Console.WriteLine("这是深圳子公司的工资计算模块"); } }
public abstract class BeiJinSalary : ISalary { public virtual void CommandSalary() { Console.WriteLine("这是北京子公司的工资计算模块"); } }
public class BeiJinItdSalary : BeiJinSalary { public override void CommandSalary() { Console.WriteLine("这是北京子公司ITD的工资计算模块"); } } public class BeiJinHraSalary : BeiJinSalary { public override void CommandSalary() { Console.WriteLine("这是北京子公司HRA的工资计算模块"); } }
public class ShenZhenItdSalary : ShenZhenSalary { public override void CommandSalary() { Console.WriteLine("这是深圳子公司ITD的工资计算模块"); } } public class ShenZhenHraSalary : ShenZhenSalary { public override void CommandSalary() { Console.WriteLine("这是深圳子公司HRA的工资计算模块"); } }
public interface IFactory { ShenZhenSalary CreateShenZhenSalary(); BeiJinSalary CreateBeiJinSalary(); }
public class ItdFactory : IFactory { public ShenZhenSalary CreateShenZhenSalary() { return new ShenZhenItdSalary(); } public BeiJinSalary CreateBeiJinSalary() { return new BeiJinItdSalary(); } }
public class HraFactory:IFactory { public ShenZhenSalary CreateShenZhenSalary() { return new ShenZhenHraSalary(); } public BeiJinSalary CreateBeiJinSalary() { return new BeiJinHraSalary(); } }
class Program { public static void Main(string[] args) { IFactory Factory = new ItdFactory(); //实例化Itd工厂类 ShenZhenSalary SalarySzItd = Factory.CreateShenZhenSalary(); SalarySzItd.CommandSalary(); //调用深圳ITD公司计算过程 BeiJinSalary SalaryBjItd = Factory.CreateBeiJinSalary(); SalaryBjItd.CommandSalary(); //调用北京ITD公司计算过程 Factory = new HraFactory(); //实例化Hra工厂类 ShenZhenSalary SalarySzHra = Factory.CreateShenZhenSalary(); SalarySzHra.CommandSalary(); //调用深圳Hra公司计算过程 BeiJinSalary SalaryBjHra = Factory.CreateBeiJinSalary(); SalaryBjHra.CommandSalary(); //调用北京Hra公司计算过程 Console.ReadLine(); } }