Builder模式的缘起
- 假设创建游戏中的一个房屋House设施,该房屋的构建由几个部分组成,且各个部分要富于变化。
- 如果使用最直观的设计方法,每一个房屋部分的变化,都将导致房屋构建的重新修正……
动机(Motivation)
- 在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。
- 如何应对这种变化?如何提供一种“封装机制”来隔离出“复杂对象的各个部分”的变化,从而保持系统中的“稳定构建算法”不随着需求改变而改变?
意图(Intent)
将一个复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。——《设计模式》GoF
结构(Structure)
协作(Collaborations)
游戏框架中的Builder应用
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
namespace testWpf
{
public abstract class House { }
public abstract class Door { }
public abstract class Wall { }
public abstract class Windows { }
public abstract class Floor { }
public abstract class HouseCeiling { }
public abstract class Builder
{
public abstract void BuildingDoor();
public abstract void BuildingWall();
public abstract void BuildingWindows();
public abstract void BuildingFloor();
public abstract void BuildingHouseCeiling(); //屋顶
public abstract House GetHouse();
}
public class GameManager
{
/// <summary>
/// 稳定的构建算法
/// </summary>
public static House CreateHouse(Builder builder)
{
builder.BuildingDoor();
builder.BuildingDoor();
builder.BuildingWall();
builder.BuildingWall();
builder.BuildingWindows();
builder.BuildingWindows();
builder.BuildingFloor();
builder.BuildingHouseCeiling();
return builder.GetHouse();
}
}
#region 不同风格的房子
public class RomainHouse : House
{
}
public class RomainDoor : Door
{
}
public class RomainWall : Wall
{
}
public class RomainWindows : Windows
{
}
public class RomainFloor : Floor
{
}
public class RomainHouseCeiling : HouseCeiling
{
}
public class RomainHouse : House
{
}
public class RomainHouseBuilder : Builder
{
public override void BuildingDoor()
{
}
public override void BuildingWall()
{
}
public override void BuildingWindows()
{
}
public override void BuildingFloor()
{
}
public override void BuildingHouseCeiling()
{
}
public override House GetHouse()
{
return new RomainHouse();
}
}
#endregion
#region Client
public class App
{
public static void Main()
{
#region 一般使用方法
//House Otherhouse = GameManager.CreateHouse(new OtherhouseBuilder()); //其它风格的房子
House house = GameManager.CreateHouse(new RomainHouseBuilder()); //罗马风格的房子
#endregion
#region 使用.net发射机制,动态构建
string assemblyName = ConfigurationSettings.AppSettings["BuilderAssembly"];
string builderName = ConfigurationSettings.AppSettings["BuildingerClass"];
Assembly assembly = Assembly.Load(assemblyName);
Type t = assembly.GetType(builderName);
Builder builder = (Builder)Activator.CreateInstance(t);
House Hhouse = GameManager.CreateHouse(builder);
#endregion
}
}
#endregion
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
namespace testWpf
{
public abstract class House { }
public abstract class Door { }
public abstract class Wall { }
public abstract class Windows { }
public abstract class Floor { }
public abstract class HouseCeiling { }
public abstract class Builder
{
public abstract void BuildingDoor();
public abstract void BuildingWall();
public abstract void BuildingWindows();
public abstract void BuildingFloor();
public abstract void BuildingHouseCeiling(); //屋顶
public abstract House GetHouse();
}
public class GameManager
{
/// <summary>
/// 稳定的构建算法
/// </summary>
public static House CreateHouse(Builder builder)
{
builder.BuildingDoor();
builder.BuildingDoor();
builder.BuildingWall();
builder.BuildingWall();
builder.BuildingWindows();
builder.BuildingWindows();
builder.BuildingFloor();
builder.BuildingHouseCeiling();
return builder.GetHouse();
}
}
#region 不同风格的房子
public class RomainHouse : House
{
}
public class RomainDoor : Door
{
}
public class RomainWall : Wall
{
}
public class RomainWindows : Windows
{
}
public class RomainFloor : Floor
{
}
public class RomainHouseCeiling : HouseCeiling
{
}
public class RomainHouse : House
{
}
public class RomainHouseBuilder : Builder
{
public override void BuildingDoor()
{
}
public override void BuildingWall()
{
}
public override void BuildingWindows()
{
}
public override void BuildingFloor()
{
}
public override void BuildingHouseCeiling()
{
}
public override House GetHouse()
{
return new RomainHouse();
}
}
#endregion
#region Client
public class App
{
public static void Main()
{
#region 一般使用方法
//House Otherhouse = GameManager.CreateHouse(new OtherhouseBuilder()); //其它风格的房子
House house = GameManager.CreateHouse(new RomainHouseBuilder()); //罗马风格的房子
#endregion
#region 使用.net发射机制,动态构建
string assemblyName = ConfigurationSettings.AppSettings["BuilderAssembly"];
string builderName = ConfigurationSettings.AppSettings["BuildingerClass"];
Assembly assembly = Assembly.Load(assemblyName);
Type t = assembly.GetType(builderName);
Builder builder = (Builder)Activator.CreateInstance(t);
House Hhouse = GameManager.CreateHouse(builder);
#endregion
}
}
#endregion
}
Builder 模式的几个要点
- Builder 模式主要用于“分步骤构建一个复杂的对象”。在这其中“分步骤”是一个稳定的算法,而复杂对象的各个部分则经常变化。
- 变化点在哪里,封装哪里—— Builder模式主要在于应对“复杂对象各个部分”的频繁需求变动。其缺点在于难以应对“分步骤构建算法”的需求变动。
- Abstract Factory模式解决“系列对象”的需求变化,Builder模式解决“对象部分”的需求变化。Builder模式通常和Composite模式组合使用。