设计模式(二)Abstract Factory
概述
在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作;同时由于需求的变化,往往存在着更多系列对象的创建工作。如何应对这种变化?如何绕过常规的对象的创建方法(new),提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合?这就是我们要说的抽象工厂模式。
意图
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
主函数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace AbstractFactoryDemo { class Program { static void Main(string[] args) { AbstractFactory factory = AbstractFactory.GetInstance(); IBonus bonus= factory.CreateBonus(); ITax tax = factory.CreateTax(); Console.WriteLine(tax.Calculate().ToString()); Console.WriteLine(bonus.Calculate().ToString()); Console.Read(); } } }
接口
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AbstractFactoryDemo { public interface ITax { double Calculate(); } public interface IBonus { double Calculate(); } }
对象1
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AbstractFactoryDemo { public class ChineseBonus : IBonus { public double Calculate() { return Constant.Base_SALARY * 0.1; } } public class ChineseTax : AbstractFactoryDemo.ITax { public double Calculate() { return (Constant.Base_SALARY + (Constant.Base_SALARY * 0.1)) * 0.4; } } }
对象2
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AbstractFactoryDemo { public class AmericanBonus:IBonus { public double Calculate() { return Constant.Base_SALARY * 0.1; } } public class AmericanTax: ITax { public double Calculate() { return (Constant.Base_SALARY + (Constant.Base_SALARY * 0.1)) * 0.5; } } }
利用了反射机制实现的Abstract Factory
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; namespace AbstractFactoryDemo { //创建静态常量类 public static class Constant { public static double Base_SALARY = 4000; public static string STR_FACTORYNAME = "AbstractFactoryDemo.ChineseFactory"; } public abstract class AbstractFactory { public static AbstractFactory GetInstance() { string factoryName = Constant.STR_FACTORYNAME; //利用反射机制实现字符串创建对象 AbstractFactory instance; Type typeGetType = Type.GetType(factoryName); if (factoryName != "") instance = (AbstractFactory)Activator.CreateInstance(typeGetType); //instance = (AbstractFactory)Assembly.Load(factoryName).CreateInstance(factoryName); else instance = null; return instance; } public abstract ITax CreateTax(); public abstract IBonus CreateBonus(); } public class ChineseFactory : AbstractFactory { public override ITax CreateTax() { return new ChineseTax() ; } public override IBonus CreateBonus() { return new ChineseBonus(); } } public class AmericanFactory : AbstractFactory { public override ITax CreateTax() { return new AmericanTax(); } public override IBonus CreateBonus() { return new AmericanBonus(); } } }
|--AbstractFactory getInstance(); // 利用反射机制获取具体对象CreateTax();CreateBonus();
|---chineseFactory ITax CreateTax();IBonus CreateBonus();
|---AmericanFactory ITax CreateTax();IBonus CreateBonus();
|--ITax
ChineseTax:ITax;
AmericanTax:ITax;
|--IBonus
chineseBonus:IBonus;
AmericanBonus:IBonus;