模式定义
造者模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
UML类图
- Builder:抽象建造者
- 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(); } }