模式定义
造者模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
UML类图
- Builder:抽象建造者 定义一个产品对象的各个部件接口和返回对象方法
getResult()
- ConcreteBuilder:具体建造者 实现抽象建造者定义的接口,需要关联产品角色,为产品部件组装
- Director:指挥者 负责安排复杂对象的建造次序,指挥者依赖抽象创建者
- Product:产品角色 构建的复杂对象,包含多个组成部件
代码结构
public static class BuilderApp
{
public static void Run()
{
Director director = new Director();
Builder b1 = new ConcreteBuilder1();
Builder b2 = new ConcreteBuilder2();
director.Construct(b1);
Product p1 = b1.GetResult();
p1.Show();
director.Construct(b2);
Product p2 = b2.GetResult();
p2.Show();
}
}
public class Product
{
private List<string> _parts = new List<string>();
public void Add(string part)
{
_parts.Add(part);
}
public void Show()
{
Console.WriteLine("\\n Product Parts -----");
foreach (string part in _parts)
{
Console.WriteLine(part);
}
}
}
public abstract class Builder
{
public abstract void BuildPartA();
public abstract void BuildPartB();
public abstract Product GetResult();
}
public class ConcreteBuilder1 : Builder
{
private Product _product = new Product();
public override void BuildPartA()
{
_product.Add("PartA");
}
public override void BuildPartB()
{
_product.Add("PartB");
}
public override Product GetResult()
{
return _product;
}
}
public class ConcreteBuilder2 : Builder
{
private Product _product = new Product();
public override void BuildPartA()
{
_product.Add("PartX");
}
public override void BuildPartB()
{
_product.Add("PartY");
}
public override Product GetResult()
{
return _product;
}
}
public class Director
{
public void Construct(Builder builder)
{
builder.BuildPartA();
builder.BuildPartB();
}
}
情景模式
本次再拿麦当劳举例(不过我是比较喜欢吃中餐的)。麦当劳中的经典套餐有:鸡腿套餐、巨无霸套餐。套餐做为产品(Product)有汉堡、小食、饮品组成,厨师为具体建造者(ConcreteBuilder),顾客为指挥者(Director)。
public static class BuilderRealWorldApp
{
public static void Run()
{
Customer customer = new Customer();
Cook b1 = new ChickenPackageCook();
Cook b2 = new BigMacCook();
customer.Construct(b1);
Package p1 = b1.GetResult();
p1.Show();
customer.Construct(b2);
Package p2 = b2.GetResult();
p2.Show();
}
}
public class Package
{
private string _name = string.Empty;
private List<string> _parts = new List<string>();
public void Add(string part)
{
_parts.Add(part);
}
public Package(string name)
{
_name = name;
}
public void Show()
{
Console.WriteLine("\\n {0} Parts is as below: -----",_name);
foreach (string part in _parts)
{
Console.WriteLine(part);
}
}
}
public abstract class Cook
{
public abstract void Burger();
public abstract void Snack();
public abstract void Drink();
public abstract Package GetResult();
}
public class ChickenPackageCook : Cook
{
private Package _product = new Package("Chicken Package");
public override void Burger()
{
_product.Add("Chicken Burger");
}
public override void Snack()
{
_product.Add("French fries");
}
public override void Drink()
{
_product.Add("Milk");
}
public override Package GetResult()
{
return _product;
}
}
public class BigMacCook : Cook
{
private Package _product = new Package("Big Mac Package");
public override void Burger()
{
_product.Add("Big Mac Burger");
}
public override void Snack()
{
_product.Add("Vegetables");
}
public override void Drink()
{
_product.Add("Cola");
}
public override Package GetResult()
{
return _product;
}
}
public class Customer
{
public void Construct(Cook builder)
{
builder.Burger();
builder.Snack();
builder.Drink();
}
}
扩展
1、省略抽象建造者角色:
如果系统中只需要一个具体建造者的话,可以省略掉抽象建造者。
2、省略指挥者角色:
在具体建造者只有一个的情况下,如果抽象建造者角色已经被省略掉,那么还可以省略指挥者角色,
让Builder角色扮演指挥者与建造者双重角色。