抽象工厂Abstract Factory
动机(Motivation)
在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作;同时,由于需求的变化,往往存在更多系列对象的创建工作。
如何应对这种变化?如何绕过常规的对象创建方法(new),提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合?
意图(Intent)
提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类。——《设计模式》GoF
结构(Structure)
Abstract Factory模式的几个要点
- 如果没有应对“多系列对象构建”的需求变化,则没有必要使用Abstract Factory模式,这时候使用简单的静态工厂完全可以。
- “系列对象”指的是这些对象之间有相互依赖、或作用的关系,例如游戏开发场景中的“道路”与“房屋”的依赖,道路”与“地道”的依赖。
- Abstract Factory模式主要在于应对“新系列”的需求变动。其缺点在于难以应对“新对象”的需求变动。
- Abstract Factory模式经常和Factory Method模式共同组合来应对“对象创建”的需求变化。
代码例子:
代码
#region 抽象类开始
//道路
public abstract class Road
{
}
//房屋
public abstract class Building
{
}
//地道
public abstract class Tunnel
{
}
//丛林
public abstract class Jungle
{
}
//设施工厂
abstract class FacilitiesFactory
{
public abstract Road CreateRoad();
public abstract Building CreateBuilding();
public abstract Tunnel CreateTunnel();
public abstract Jungle CreateJungle();
}
#endregion 抽象类结束
#region 具体类型1 开始。一系列相互依赖的对象
//道路
public class ModernRoad : Road
{
}
//房屋
public class ModernBuilding : Building
{
}
//地道
public class ModernTunnel : Tunnel
{
}
//丛林
public class ModernJungle : Jungle
{
}
//设施工厂
public class ModernFacilitiesFactory : FacilitiesFactory
{
public override Road CreateRoad()
{
return new ModernRoad();
}
public override Building CreateBuilding()
{
return new ModernRoad();
}
public override Tunnel CreateTunnel()
{
return new ModernTunnel();
}
public override Jungle CreateJungle()
{
return new ModernJungle();
}
}
#region 具体类型1 结束
//客户程序
class GameManager
{
FacilitiesFactory facilitiesFactory;
Road road;
Building building;
Tunnel tunnel;
Jungle jungle;
public GameManager(FacilitiesFactory facilitiesFactory)
{
this.facilitiesFactory = facilitiesFactory;
}
/// <summary>
/// 都是依赖抽象方法
/// </summary>
public void BuildingGameFacilities()
{
road = facilitiesFactory.CreateRoad();
building = facilitiesFactory.CreateBuilding();
tunnel = facilitiesFactory.CreateTunnel();
jungle = facilitiesFactory.CreateJungle();
}
public void Run()
{
//程序的具体逻辑
road.AAA();
building.BBB();
tunnel.CCC();
jungle.DDD(tunnel);
}
}
class App
{
public static void Main()
{
//GameManger g = new GameManager(new OtherFacilitiesFactory()); //第一种系统风格
GameManger g = new GameManager(new ModernFacilitiesFactory()); //第二种系统风格
g.BuildingGameFacilities();
g.Run();
}
}
//道路
public abstract class Road
{
}
//房屋
public abstract class Building
{
}
//地道
public abstract class Tunnel
{
}
//丛林
public abstract class Jungle
{
}
//设施工厂
abstract class FacilitiesFactory
{
public abstract Road CreateRoad();
public abstract Building CreateBuilding();
public abstract Tunnel CreateTunnel();
public abstract Jungle CreateJungle();
}
#endregion 抽象类结束
#region 具体类型1 开始。一系列相互依赖的对象
//道路
public class ModernRoad : Road
{
}
//房屋
public class ModernBuilding : Building
{
}
//地道
public class ModernTunnel : Tunnel
{
}
//丛林
public class ModernJungle : Jungle
{
}
//设施工厂
public class ModernFacilitiesFactory : FacilitiesFactory
{
public override Road CreateRoad()
{
return new ModernRoad();
}
public override Building CreateBuilding()
{
return new ModernRoad();
}
public override Tunnel CreateTunnel()
{
return new ModernTunnel();
}
public override Jungle CreateJungle()
{
return new ModernJungle();
}
}
#region 具体类型1 结束
//客户程序
class GameManager
{
FacilitiesFactory facilitiesFactory;
Road road;
Building building;
Tunnel tunnel;
Jungle jungle;
public GameManager(FacilitiesFactory facilitiesFactory)
{
this.facilitiesFactory = facilitiesFactory;
}
/// <summary>
/// 都是依赖抽象方法
/// </summary>
public void BuildingGameFacilities()
{
road = facilitiesFactory.CreateRoad();
building = facilitiesFactory.CreateBuilding();
tunnel = facilitiesFactory.CreateTunnel();
jungle = facilitiesFactory.CreateJungle();
}
public void Run()
{
//程序的具体逻辑
road.AAA();
building.BBB();
tunnel.CCC();
jungle.DDD(tunnel);
}
}
class App
{
public static void Main()
{
//GameManger g = new GameManager(new OtherFacilitiesFactory()); //第一种系统风格
GameManger g = new GameManager(new ModernFacilitiesFactory()); //第二种系统风格
g.BuildingGameFacilities();
g.Run();
}
}