C#设计模式_创建型_AbstractFactory模式(learn from Lijianzhong & others)
Posted on 2007-01-28 23:58 taoeternal 阅读(291) 评论(0) 编辑 收藏 举报意图:提供一个创建一系列相关或相互依赖对象的接口,而无须制定它们具体的类
适用性:
using System;
namespace DesignPattern
{
/// <summary>
/// AbstractFactory 的摘要说明。
/// 动机:软件系统中,经常面临着“一系列相互依赖的对象”的创建工作
/// 同时,由于需求的变化,往往存在更多系列对象的创建工作
/// 意图:提供一个接口,让该接口负责创建一系列“相关或者相互依赖的
/// 对象”,无需制定它们具体的类
/// 要点:1、如果没有应对“多系列对象构建”的需求变化,则没必要使用此
/// 模式,此时使用简单的静态工厂完全可以
/// 2、“系列对象”指的是这些对象之间有相互依赖、或作用的关系
/// ,例如游戏开发场景中的“道路”与“房屋”的以来,“道路”与
/// “地道”的依赖
/// 3、此模式主要应对“新系列”的需求变动,其缺点在于难以应对
/// “新对象”的需求变动
/// 4、此模式经常和Factory Mode模式共同组合来应对“对象创建”的
/// 需求变化
/// 5、根据需求变化的轴线来应用
/// NEW 的问题
/// 常规对象创建方法:Road road = new Road();
/// 问题:实现依赖,不能应对“具体实例化类型”的变化
/// 解决:封装变化点——哪里变化封装哪里
/// 工厂模式的缘起:
/// 变化点在“对象创建”,因此封装对象创建
/// 面向接口编程——依赖接口,而非实现
/// 最简单的解决方法:
/// class RoadFactory{//类库
/// public static Road CreateRoad()
/// {
/// return new Road();
/// }
/// //创建一个Road对象,客户层应用
/// Road road = roadFactory.CreateRoad();
/// </summary>
//道路
public abstract class Road
{
}
//房屋
public abstract class Building
{
}
//地道
public abstract class Tunnel
{
}
//丛林
public abstract class Jungle
{
}
public abstract class FacilitiesFactory
{
public abstract Road CreateRoad();
public abstract Building CreateBuilding();
public abstract Tunnel CreateTunnel();
public abstract Jungle CreateJungle();
}
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 ModernBuilding();
}
public override Tunnel CreateTunnel(){
return new ModernTunnel();
}
public override Jungle CreateJungle(){
return new ModernJungle();
}
}
public class ClassicRoad : Road
{
}
public class ClassicBuilding : Building
{
}
public class ClassicTunnel : Tunnel
{
}
public class ClassicJungle : Jungle
{
}
public class ClassicFacilitiesFactory : FacilitiesFactory
{
public override Road CreateRoad()
{
return new ClassicRoad();
}
public override Building CreateBuilding()
{
return new ClassicBuilding();
}
public override Tunnel CreateTunnel()
{
return new ClassicTunnel();
}
public override Jungle CreateJungle()
{
return new ClassicJungle();
}
}
//客户程序
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.CreateBuilding();
tunnel = facilitiesFactory.CreateTunnel();
jungle = facilitiesFactory.CreateJungle();
}
public void Run()
{
// road.AAA();
// building.BBB(road);
// tunnel.CCC();
// jungle.DDD(tunnel);
}
}
// class App
// {
// public static void Main()
// {
// GameManager gm = new GameManager(new ModernFacilitiesFactory());
// gm.BuildGameFacilities();
// gm.Run();
// }
// }
}