建造者模式(Builder Pattern)
阿新 • • 發佈:2018-01-25
png ole col pac world 不同 ons console img
模式定義
造者模式(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(); } }
建造者模式(Builder Pattern)