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

抽象工厂

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

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

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

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

     public interface IRace
{
/// <summary>
/// show出王者
/// </summary>
void ShowKing();
}
public interface IArmy
{
void ShowArmy();
}
public interface IHero
{
void ShowHero();
}
public interface ILuck
{
void ShowLuck();
}
public interface IResource
{
void ShowResource();
}
     /// <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");
}
}
     /// <summary>
/// War3种族之一
/// </summary>
public class ORC : IRace
{
public void ShowKing()
{
Console.WriteLine("The King of {0} is {1}", this.GetType().Name, "Grubby");
}
} public class ORCArmy : IArmy
{ public void ShowArmy()
{
Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "大G、风骑士、蝙蝠、战车、牛头人");
}
} public class ORCHero : IHero
{
public void ShowHero()
{
Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "剑圣、小萨满、先知、牛头人酋长");
}
}
public class ORCResource : IResource
{ public void ShowResource()
{
Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "1000G1000W");
}
}
     /// <summary>
/// War3种族之一
/// </summary>
public class Undead : IRace
{
public void ShowKing()
{
Console.WriteLine("The King of {0} is {1}", this.GetType().Name, "GoStop");
}
} public class UndeadArmy : IArmy
{ public void ShowArmy()
{
Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "食尸鬼,蜘蛛,雕像,战车,憎恶,冰霜巨龙");
}
} public class UndeadHero : IHero
{
public void ShowHero()
{
Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "DK、Lich、小强、恐惧魔王");
}
}
public class UndeadResource : IResource
{ public void ShowResource()
{
Console.WriteLine("The Army of {0} is {1}", this.GetType().Name, "1000G1000W");
}
}
     /// <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();
}

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

工厂+约束
工厂

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

工厂方法与抽象工厂

1、工厂方法

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

2、抽象工厂

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

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

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

上一篇:今天突然能用了


下一篇:GCC下32位与64位机器类型变量所占字节数