23种设计模式之抽象工厂(Abstract Factory Pattern)

抽象工厂

当想创建一组密不可分的对象时,工厂方法似乎就不够用了

抽象工厂是应对产品族概念的。应对产品族概念而生,增加新的产品线很容易,但是无法增加新的产品。比如,每个汽车公司可能要同时生产轿车、货车、客车,那么每一个工厂都要有创建轿车、货车和客车的方法

优点:向客户端提供一个接口,使得客户端在不必指定产品具体类型的情况下,创建多个产品族中的产品对象

缺点:增加新的产品等级结构很复杂,需要修改抽象工厂和所有的具体工厂类,对“开闭原则”的支持呈现倾斜性

 1     public interface IRace
 2     {
 3         /// <summary>
 4         /// show出王者
 5         /// </summary>
 6         void ShowKing();
 7     }
 8     public interface IArmy
 9     {
10         void ShowArmy();
11     }
12     public interface IHero
13     {
14         void ShowHero();
15     }
16     public interface ILuck
17     {
18         void ShowLuck();
19     }
20     public interface IResource
21     {
22         void ShowResource();
23     }

View Code

 1     /// <summary>
 2     /// War3种族之一
 3     /// </summary>
 4     public class Human : IRace
 5     {
 6         public Human(int id, DateTime dateTime, string reamrk)
 7         { }
 8         public Human()
 9         { }
10 
11         public void ShowKing()
12         {
13             Console.WriteLine("The King of {0} is {1}", this.GetType().Name, "Sky");
14         }
15     }
16 
17     public class HumanArmy : IArmy
18     {
19 
20         public void ShowArmy()
21         {
22             Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "footman,火枪,骑士,狮鹫");
23         }
24     }
25 
26     public class HumanHero : IHero
27     {
28         public void ShowHero()
29         {
30             Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "大法师、山丘、圣骑士、血法师");
31         }
32     }
33     public class HumanResource : IResource
34     {
35 
36         public void ShowResource()
37         {
38             Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "1000G1000W");
39         }
40     }

View Code

 1     /// <summary>
 2     /// War3种族之一
 3     /// </summary>
 4     public class ORC : IRace
 5     {
 6         public void ShowKing()
 7         {
 8             Console.WriteLine("The King of {0} is {1}", this.GetType().Name, "Grubby");
 9         }
10     }
11 
12     public class ORCArmy : IArmy
13     {
14 
15         public void ShowArmy()
16         {
17             Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "大G、风骑士、蝙蝠、战车、牛头人");
18         }
19     }
20 
21     public class ORCHero : IHero
22     {
23         public void ShowHero()
24         {
25             Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "剑圣、小萨满、先知、牛头人酋长");
26         }
27     }
28     public class ORCResource : IResource
29     {
30 
31         public void ShowResource()
32         {
33             Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "1000G1000W");
34         }
35     }

View Code

 1     /// <summary>
 2     /// War3种族之一
 3     /// </summary>
 4     public class Undead : IRace
 5     {
 6         public void ShowKing()
 7         {
 8             Console.WriteLine("The King of {0} is {1}", this.GetType().Name, "GoStop");
 9         }
10     }
11 
12     public class UndeadArmy : IArmy
13     {
14 
15         public void ShowArmy()
16         {
17             Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "食尸鬼,蜘蛛,雕像,战车,憎恶,冰霜巨龙");
18         }
19     }
20 
21     public class UndeadHero : IHero
22     {
23         public void ShowHero()
24         {
25             Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "DK、Lich、小强、恐惧魔王");
26         }
27     }
28     public class UndeadResource : IResource
29     {
30 
31         public void ShowResource()
32         {
33             Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "1000G1000W");
34         }
35     }

View Code

 1     /// <summary>
 2     /// 一个工厂负责一些产品的创建
 3     /// 产品簇
 4     /// 单一职责就是创建完整的产品簇
 5     /// 
 6     /// 继承抽象类后,必须显式的override父类的抽象方法
 7     /// </summary>
 8     public abstract class FactoryAbstract
 9     {
10         public abstract IRace CreateRace();
11         public abstract IArmy CreateArmy();
12         public abstract IHero CreateHero();
13         public abstract IResource CreateResource();
14 
15         //public abstract ILuck CreateLuck();
16     }

View Code

 1     /// <summary>
 2     /// 一个工厂负责一些产品的创建
 3     /// </summary>
 4     public class HumanFactory : FactoryAbstract
 5     {
 6         public override IRace CreateRace()
 7         {
 8             return new Human();
 9         }
10 
11         public override IArmy CreateArmy()
12         {
13             return new HumanArmy();
14         }
15         public override IHero CreateHero()
16         {
17             return new HumanHero();
18         }
19         public override IResource CreateResource()
20         {
21             return new HumanResource();
22         }
23     }

View Code

 1     /// <summary>
 2     /// 一个工厂负责一些产品的创建
 3     /// </summary>
 4     public class ORCFactory : FactoryAbstract
 5     {
 6         public override IRace CreateRace()
 7         {
 8             return new ORC();
 9         }
10 
11         public override IArmy CreateArmy()
12         {
13             return new ORCArmy();
14         }
15         public override IHero CreateHero()
16         {
17             return new ORCHero();
18         }
19         public override IResource CreateResource()
20         {
21             return new ORCResource();
22         }
23     }

View Code

 1     /// <summary>
 2     /// 一个工厂负责一些产品的创建
 3     /// </summary>
 4     public class UndeadFactory : FactoryAbstract
 5     {
 6         public override IRace CreateRace()
 7         {
 8             return new Undead();
 9         }
10 
11         public override IArmy CreateArmy()
12         {
13             return new UndeadArmy();
14         }
15         public override IHero CreateHero()
16         {
17             return new UndeadHero();
18         }
19         public override IResource CreateResource()
20         {
21             return new UndeadResource();
22         }
23     }

View Code

前端调用

 1                {
 2                     IRace race = new Undead();
 3                     IArmy army = new UndeadArmy();
 4                     IHero hero = new UndeadHero();
 5                     IResource resource = new UndeadResource();
 6                     //1 对象转移,屏蔽细节,让使用者更轻松
 7                     //2 对象簇的工厂
 8                 }
 9                 {
10 
11                     //System.Data.SqlClient.SqlClientFactory
12                     FactoryAbstract undeadFactory = new UndeadFactory();
13                     IRace race = undeadFactory.CreateRace();// new Undead();
14                     IArmy army = undeadFactory.CreateArmy();//new UndeadArmy();
15                     IHero hero = undeadFactory.CreateHero();//new UndeadHero();
16                     IResource resource = undeadFactory.CreateResource();//new UndeadResource();
17                 }
18                 {
19                     FactoryAbstract humanFactory = new HumanFactory();
20                     IRace race = humanFactory.CreateRace();
21                     IArmy army = humanFactory.CreateArmy();
22                     IHero hero = humanFactory.CreateHero();
23                     IResource resource = humanFactory.CreateResource();
24                 }

View Code

抽象工厂:创建对象,创建一组密不可分的对象
 创建产品簇,多个对象是个整体,不可分割

工厂+约束
工厂

倾斜的可扩展性设计
扩展种族很方便
增加产品很麻烦

工厂方法与抽象工厂

1、工厂方法

       一个抽象产品类,可以派生出多个具体产品类。一个抽象工厂类,可以派生出多个具体工厂类。每个具体工厂类只能创建一个具体产品类的实例

2、抽象工厂

        多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。一个抽象工厂类,可以派生出多个具体工厂类。每个具体工厂类可以创建多个具体产品类的实例

3、工厂方法与抽象工厂的区别

        工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。工厂方法模式的具体工厂类只能创建一个具体产品类的实例,而抽象工厂模式可以创建多个
本文参考文档:https://blog.csdn.net/liuziteng0228/article/details/54849055

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注