13_建造者模式
2016-11-24 15:01:20 18 举报
《大话设计模式》-建造者模式
作者其他创作
大纲/内容
Director
+Construct()
ConcreteBuilder
+BuildPart()+GetResult()
Product
-builder
static void Main(string[] args) { 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(); Console.Read(); }
class Product { IList parts = new List(); public void Add(string part) { parts.Add(part); } public void Show() { Console.WriteLine(\"\产品 创建 ----\"); foreach (string part in parts) { Console.WriteLine(part); } } }
abstract class Builder { public abstract void BuildPartA(); public abstract void BuildPartB(); public abstract Product GetResult(); }
Builder
+BuildPart()
class ConcreteBuilder2 : Builder { private Product product = new Product(); public override void BuildPartA() { product.Add(\"部件X\"); } public override void BuildPartB() { product.Add(\"部件Y\"); } public override Product GetResult() { return product; } }
Client
class Director { public void Construct(Builder builder) { builder.BuildPartA(); builder.BuildPartB(); } }
class ConcreteBuilder1 : Builder { private Product product = new Product(); public override void BuildPartA() { product.Add(\"部件A\"); } public override void BuildPartB() { product.Add(\"部件B\"); } public override Product GetResult() { return product; } }
调用顺序
0 条评论
下一页