new的问题
----new实现依赖,不能应对具体实例化类型的变化
面向接口编成-----依赖借口,而非依赖实现
动机:
在软件系统中,经常面临着“一系列相互依赖的对象”的创建工作;同时,由于需求的变化,往往存在更多系列对象的创建工作。
如何应对这种变化?如何绕过常规的对象创建方法(new),提供一种“封装机制”来避免客房程序和这种“多系列具体对象创建工作”的支耦合?
意图:
提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类。
出自:《设计模式》GoF
Abstract Factory模式的几个要点:
1、如果没有应对“多系列对象构建”的需求变化,则没有必要使用Abstract Factroy模式,这里使用简单的静态工厂完全可以。
2、“系列对象”指的是这些对象之间有相互依赖、或作用的关系,例如游戏开发场景中的“道路”与“房屋”的依赖,“道路”与“地道”的依赖。
3、Abstract Factory模式主要在于应对“新系列”的需求变动。其缺点在于难以应用“新对象”的需求变动。
4、Abstract Factory模式经常和Factory Method模式共同组合来应对“对象创建”的需求变化。
//道路
public abstract class Road
{
public abstract void Do();
}
//房屋
public abstract class Building
{
public abstract void Do();
}
//地道
public abstract class Tunnel
{
public abstract void Do();
}
//从林
public abstract class Jungle
{
public abstract void Do();
}
//抽象工厂
public abstract class FacilitiesFactory
{
public abstract Road CreateRoad();
public abstract Buliding CreateBuliding();
public abstract Tunnel CreateTunnel();
public abstract Jungle CreateJungle();
}
//客户程序
public class GameManager
{
FacilitiesFactory facilitiesFactory;
Road road;
Building building;
Tunnel tunnel;
Jungle jungle;
public GameManager(FacilitiesFactory facilitiesFactory)
{
this.facilitiesFactory = facilitiesFactory;
}
public Void BuildGameFacilities()
{
road = facilitiesFactory.CreateRoad();
building = facilitiesFactory.CreateBuliding();
tunnel = facilitiesFactory.CreateTunnel();
jungle = facilitiesFactory.CreateTunnel();
}
public void Run()
{
road.Do();
building.Do(road);//各个对象之间的依赖关系
tunnel.Do();
jungle.Do();
}
}
//变化部分:
public class RoadA:Road
{
public void Do()
{
}
}
public class BuildingA:Building
{
public void Do(Road road)
{
}
}
public class TunnelA:Tunnel
{
public void Do()
{
}
}
public class JungleA:Jungle
{
public void Do()
{
}
}
public class FacilitiesFactoryA:FacilitiesFactory
{
public override Road CreateRoad()
{
return new RoadA();
}
public override Buliding CreateBuliding()
{
return new BulidingA();
}
public override Tunnel CreateTunnel()
{
return new TunnelA();
}
public override Jungle CreateJungle()
{
return new JungleA();
}
}
//主程序:
//稳定部分:系列对象的组合(以及组合的行为)是稳定的.如这里的Road,Building,Tunnel,Jungle
//应用于"系列对象"的变化
public static void Main()
{
//变化点
GameManager g = new GameManager(new FacilitiesFactoryA());
//GameManager g = new GameManager(new FacilitiesFactoryB());
g.BuildGameFacilities();
g.Run();
}
public abstract class Road
{
public abstract void Do();
}
//房屋
public abstract class Building
{
public abstract void Do();
}
//地道
public abstract class Tunnel
{
public abstract void Do();
}
//从林
public abstract class Jungle
{
public abstract void Do();
}
//抽象工厂
public abstract class FacilitiesFactory
{
public abstract Road CreateRoad();
public abstract Buliding CreateBuliding();
public abstract Tunnel CreateTunnel();
public abstract Jungle CreateJungle();
}
//客户程序
public class GameManager
{
FacilitiesFactory facilitiesFactory;
Road road;
Building building;
Tunnel tunnel;
Jungle jungle;
public GameManager(FacilitiesFactory facilitiesFactory)
{
this.facilitiesFactory = facilitiesFactory;
}
public Void BuildGameFacilities()
{
road = facilitiesFactory.CreateRoad();
building = facilitiesFactory.CreateBuliding();
tunnel = facilitiesFactory.CreateTunnel();
jungle = facilitiesFactory.CreateTunnel();
}
public void Run()
{
road.Do();
building.Do(road);//各个对象之间的依赖关系
tunnel.Do();
jungle.Do();
}
}
//变化部分:
public class RoadA:Road
{
public void Do()
{
}
}
public class BuildingA:Building
{
public void Do(Road road)
{
}
}
public class TunnelA:Tunnel
{
public void Do()
{
}
}
public class JungleA:Jungle
{
public void Do()
{
}
}
public class FacilitiesFactoryA:FacilitiesFactory
{
public override Road CreateRoad()
{
return new RoadA();
}
public override Buliding CreateBuliding()
{
return new BulidingA();
}
public override Tunnel CreateTunnel()
{
return new TunnelA();
}
public override Jungle CreateJungle()
{
return new JungleA();
}
}
//主程序:
//稳定部分:系列对象的组合(以及组合的行为)是稳定的.如这里的Road,Building,Tunnel,Jungle
//应用于"系列对象"的变化
public static void Main()
{
//变化点
GameManager g = new GameManager(new FacilitiesFactoryA());
//GameManager g = new GameManager(new FacilitiesFactoryB());
g.BuildGameFacilities();
g.Run();
}