public class Product { private String partA; private String partB; private String partC; public void SetPartA(String partA) { this.partA=partA; } public void SetPartB(String partB) { this.partB=partB; } public void SetPartC(String partC) { this.partC=partC; } public void Show() { } }
public abstract class Builder { protected Product product=new Product(); public abstract void BuildPartA(); public abstract void BuildPartB(); public abstract void BuildPartC(); public Product GetResult() { return product; } }
public class ConcreteBuilder1 : Builder { public void BuildPartA() { product.SetPartA("建造 PartA"); } public void BuildPartB() { product.SetPartA("建造 PartB"); } public void BuildPartC() { product.SetPartA("建造 PartC"); } }
public class Director { private Builder builder; public Director(Builder builder) { this.builder=builder; } public Product Construct() { builder.BuildPartA(); builder.BuildPartB(); builder.BuildPartC(); return builder.GetResult(); } }
class Program { static void Main(string[] args) { Builder builder=new ConcreteBuilder1(); Director director=new Director(builder); Product product=director.Construct(); product.Show(); Console.ReadKey(); } }