Builder生成器模式 (转)
Builder生成器模式
定义:将一个复杂对象的构建与其表示相分离,使同样的构建过程可以创建不同的表示. -------GOF
一个复 杂对象的构建,通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临剧烈的变化,但是将将它们组合在一起的算法却相对稳定.
1.主用于分步骤的构建一个复杂的对象.其中“分步骤”是一个稳定的算法,而对象的各个部份则经常 变化
2.变化哪,封装在哪
3.Abstract Factory解决系统对象的需求变化,而Builder解决对象部份的需求变化
//抽象产品类
public abstract class House
{
}
public abstract class House
{
}
//抽象部件类
public abstract class House
{
}
public abstract class Door
{
}
public abstract class Window
{
}
public abstract class Wall
{
}
public abstract class Floor
{
}
public abstract class HouseCelling
{
}
public abstract class House
{
}
public abstract class Door
{
}
public abstract class Window
{
}
public abstract class Wall
{
}
public abstract class Floor
{
}
public abstract class HouseCelling
{
}
/// <summary>
/// 抽象类,定义如何创建对象的各个部份
/// </summary>
public abstract class Builder
{
public abstract void BuilderDoor();
public abstract void BuilderWindow();
public abstract void BuilderWall();
public abstract void BuilderFloor();
public abstract void BuilderHouseCelling();
public abstract House GetHouse();
}
/// 抽象类,定义如何创建对象的各个部份
/// </summary>
public abstract class Builder
{
public abstract void BuilderDoor();
public abstract void BuilderWindow();
public abstract void BuilderWall();
public abstract void BuilderFloor();
public abstract void BuilderHouseCelling();
public abstract House GetHouse();
}
//将各个部件构建成复杂对象的构建 过程
public class GameManager
{
public static House CreateHouse(Builder builder)
{
/// 最稳定的一块,相当于算法.
//三个门
builder.BuilderDoor();
builder.BuilderDoor();
builder.BuilderDoor();
//两个窗
builder.BuilderWindow();
builder.BuilderWindow();
//一个floor
builder.BuilderFloor();
return builder.GetHouse();
}
}
public class GameManager
{
public static House CreateHouse(Builder builder)
{
/// 最稳定的一块,相当于算法.
//三个门
builder.BuilderDoor();
builder.BuilderDoor();
builder.BuilderDoor();
//两个窗
builder.BuilderWindow();
builder.BuilderWindow();
//一个floor
builder.BuilderFloor();
return builder.GetHouse();
}
}
//具体部件类
public class ChinaDoor : Door
{
}
public class ChinaWindow : Window
{
}
public class ChinaWall : Wall
{
}
public class ChinaFloor : Floor
{
}
public class ChinaHourseCelling : HouseCelling
{
}
public class ChinaDoor : Door
{
}
public class ChinaWindow : Window
{
}
public class ChinaWall : Wall
{
}
public class ChinaFloor : Floor
{
}
public class ChinaHourseCelling : HouseCelling
{
}
//具体完成各个不同部件的构建
public class ChinaStyle : Builder
{
public override void BuilderDoor()
{
//如何创建门
}
public override void BuilderWindow()
{
//如何创建窗
}
public override void BuilderWall()
{
}
public override void BuilderFloor()
{
}
public override void BuilderHouseCelling()
{
}
}
public class ChinaStyle : Builder
{
public override void BuilderDoor()
{
//如何创建门
}
public override void BuilderWindow()
{
//如何创建窗
}
public override void BuilderWall()
{
}
public override void BuilderFloor()
{
}
public override void BuilderHouseCelling()
{
}
}
//客户端块
public class Client
{
public static void Start()
{
//构建
GameManager.CreateHouse(new ChinaStyle());
//如果在Web中,此处可用配制文件信息来处理。省略变动.
string strBuilderName = ConfigurationSetting["BuilderName"];
string strAssemblyName = ConfigurationSetting["AssemblyName"];
Assembly assembly = Assembly.Loas(strAssemblyName);
Type type = Assembly.getType(BuilderName);
Builder builder = Activator.CreateInstance(type);
GameManager.CreateHouse(builder);
}
}
public class Client
{
public static void Start()
{
//构建
GameManager.CreateHouse(new ChinaStyle());
//如果在Web中,此处可用配制文件信息来处理。省略变动.
string strBuilderName = ConfigurationSetting["BuilderName"];
string strAssemblyName = ConfigurationSetting["AssemblyName"];
Assembly assembly = Assembly.Loas(strAssemblyName);
Type type = Assembly.getType(BuilderName);
Builder builder = Activator.CreateInstance(type);
GameManager.CreateHouse(builder);
}
}