模式介绍
Builder模式将对象的构造与其表示分离,以便相同的构造过程可以创建不同的表示。
一般思想是,在实例化对象时发生的事情的顺序是相同的,但是这些步骤的实际细节根据具体实现是什么而改变。
示例
比如我们现在做三明治,不同的三明治,做法步骤总归是一样的。所以我们通过同一个组装线进行处理。
Director类:生产线
/// <summary>
/// The Director
/// </summary>
class AssemblyLine
{
// Builder uses a complex series of steps
//
public void Assemble(SandwichBuilder sandwichBuilder)
{
sandwichBuilder.AddBread();
sandwichBuilder.AddMeats();
sandwichBuilder.AddCheese();
sandwichBuilder.AddVeggies();
sandwichBuilder.AddCondiments();
}
}
Product类:三明治
/// <summary>
/// The Product class
/// </summary>
class Sandwich
{
private string _sandwichType;
private Dictionary<string, string> _ingredients = new Dictionary<string, string>();
// Constructor
public Sandwich(string sandwichType)
{
this._sandwichType = sandwichType;
}
// Indexer
public string this[string key]
{
get { return _ingredients[key]; }
set { _ingredients[key] = value; }
}
public void Show()
{
Console.WriteLine("
---------------------------");
Console.WriteLine("Sandwich: {0}", _sandwichType);
Console.WriteLine(" Bread: {0}", _ingredients["bread"]);
Console.WriteLine(" Meat: {0}", _ingredients["meat"]);
Console.WriteLine(" Cheese: {0}", _ingredients["cheese"]);
Console.WriteLine(" Veggies: {0}", _ingredients["veggies"]);
Console.WriteLine(" Condiments: {0}", _ingredients["condiments"]);
}
}
Builder类:三明治Builder类,抽象类
/// <summary>
/// The Builder abstract class
/// </summary>
abstract class SandwichBuilder
{
protected Sandwich sandwich;
// Gets sandwich instance
public Sandwich Sandwich
{
get { return sandwich; }
}
// Abstract build methods
public abstract void AddBread();
public abstract void AddMeats();
public abstract void AddCheese();
public abstract void AddVeggies();
public abstract void AddCondiments();
}
具体的Builder类
/// <summary>
/// A ConcreteBuilder class
/// </summary>
class TurkeyClub : SandwichBuilder
{
public TurkeyClub()
{
sandwich = new Sandwich("Turkey Club");
}
public override void AddBread()
{
sandwich["bread"] = "12-Grain";
}
public override void AddMeats()
{
sandwich["meat"] = "Turkey";
}
public override void AddCheese()
{
sandwich["cheese"] = "Swiss";
}
public override void AddVeggies()
{
sandwich["veggies"] = "Lettuce, Tomato";
}
public override void AddCondiments()
{
sandwich["condiments"] = "Mayo";
}
}
/// <summary>
/// A ConcreteBuilder class
/// </summary>
class BLT : SandwichBuilder
{
public BLT()
{
sandwich = new Sandwich("BLT");
}
public override void AddBread()
{
sandwich["bread"] = "Wheat";
}
public override void AddMeats()
{
sandwich["meat"] = "Bacon";
}
public override void AddCheese()
{
sandwich["cheese"] = "None";
}
public override void AddVeggies()
{
sandwich["veggies"] = "Lettuce, Tomato";
}
public override void AddCondiments()
{
sandwich["condiments"] = "Mayo, Mustard";
}
}
/// <summary>
/// A ConcreteBuilder class
/// </summary>
class HamAndCheese : SandwichBuilder
{
public HamAndCheese()
{
sandwich = new Sandwich("Ham and Cheese");
}
public override void AddBread()
{
sandwich["bread"] = "White";
}
public override void AddMeats()
{
sandwich["meat"] = "Ham";
}
public override void AddCheese()
{
sandwich["cheese"] = "American";
}
public override void AddVeggies()
{
sandwich["veggies"] = "None";
}
public override void AddCondiments()
{
sandwich["condiments"] = "Mayo";
}
}
客户端调用:
static void Main(string[] args)
{
SandwichBuilder builder;
// Create shop with sandwich assembly line
AssemblyLine shop = new AssemblyLine();
// Construct and display sandwiches
builder = new HamAndCheese();
shop.Assemble(builder);
builder.Sandwich.Show();
builder = new BLT();
shop.Assemble(builder);
builder.Sandwich.Show();
builder = new TurkeyClub();
shop.Assemble(builder);
builder.Sandwich.Show();
// Wait for user
Console.ReadKey();
}
总结
Builder模式允许我们使用相同的步骤构建相关的对象集,但将这些步骤的实现留给子类。
源代码
https://github.com/exceptionnotfound/DesignPatterns/tree/master/Builder
原文
https://www.exceptionnotfound.net/builder-the-daily-design-pattern/