设计模式-抽象工厂
抽象工厂:创建对象,创建一组密不可分的对象
创建产品簇,多个对象是个整体,不可分割
工厂+约束
倾斜的可扩展性设计,增加产品很麻烦
/// <summary> /// 一个工厂负责一些产品的创建 /// 产品簇 /// 单一职责就是创建完整的产品簇 /// /// 继承抽象类后,必须显式的override父类的抽象方法 /// </summary> public abstract class FactoryAbstract { public abstract IRace CreateRace(); public abstract IArmy CreateArmy(); public abstract IHero CreateHero(); public abstract IResource CreateResource(); //public abstract ILuck CreateLuck(); }
/// <summary> /// 一个工厂负责一些产品的创建 /// </summary> public class HumanFactory : FactoryAbstract { public override IRace CreateRace() { return new Human(); } public override IArmy CreateArmy() { return new HumanArmy(); } public override IHero CreateHero() { return new HumanHero(); } public override IResource CreateResource() { return new HumanResource(); } }
/// <summary> /// 一个工厂负责一些产品的创建 /// </summary> public class ORCFactory : FactoryAbstract { public override IRace CreateRace() { return new ORC(); } public override IArmy CreateArmy() { return new ORCArmy(); } public override IHero CreateHero() { return new ORCHero(); } public override IResource CreateResource() { return new ORCResource(); } }
/// <summary> /// 一个工厂负责一些产品的创建 /// </summary> public class UndeadFactory : FactoryAbstract { public override IRace CreateRace() { return new Undead(); } public override IArmy CreateArmy() { return new UndeadArmy(); } public override IHero CreateHero() { return new UndeadHero(); } public override IResource CreateResource() { return new UndeadResource(); } }
{ IRace race = new Undead(); IArmy army = new UndeadArmy(); IHero hero = new UndeadHero(); IResource resource = new UndeadResource(); //1 对象转移,屏蔽细节,让使用者更轻松 //2 对象簇的工厂 } { //System.Data.SqlClient.SqlClientFactory FactoryAbstract undeadFactory = new UndeadFactory(); IRace race = undeadFactory.CreateRace();// new Undead(); IArmy army = undeadFactory.CreateArmy();//new UndeadArmy(); IHero hero = undeadFactory.CreateHero();//new UndeadHero(); IResource resource = undeadFactory.CreateResource();//new UndeadResource(); } { FactoryAbstract humanFactory = new HumanFactory(); IRace race = humanFactory.CreateRace(); IArmy army = humanFactory.CreateArmy(); IHero hero = humanFactory.CreateHero(); IResource resource = humanFactory.CreateResource(); } { FactoryAbstract humanFactory = new ORCFactory(); IRace race = humanFactory.CreateRace(); IArmy army = humanFactory.CreateArmy(); IHero hero = humanFactory.CreateHero(); IResource resource = humanFactory.CreateResource(); }
/// <summary> /// War3种族之一 /// </summary> public class Human : IRace { public Human(int id, DateTime dateTime, string reamrk) { } public Human() { } public void ShowKing() { Console.WriteLine("The King of {0} is {1}", this.GetType().Name, "Sky"); } } public class HumanArmy : IArmy { public void ShowArmy() { Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "footman,火枪,骑士,狮鹫"); } } public class HumanHero : IHero { public void ShowHero() { Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "大法师、山丘、圣骑士、血法师"); } } public class HumanResource : IResource { public void ShowResource() { Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "1000G1000W"); } }