抽象工廠:創建對象,創建一組密不可分的對象 創建產品簇,多個對象是個整體,不可分割 工廠+約束 傾斜的可擴展性設計,增加產品很麻煩 /// <summary> /// War3種族之一 /// </summary> public class Human : IRace { public Human( ...
抽象工廠:創建對象,創建一組密不可分的對象
創建產品簇,多個對象是個整體,不可分割
工廠+約束
傾斜的可擴展性設計,增加產品很麻煩
/// <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"); } }View Code