应用场景:产品对象的构造比较复杂时使用。
using System; using System.Collections.Generic; using System.Text; namespace BuilderTest { class Program { static void Main(string[] args) { ConcreteBuilder cb = new ConcreteBuilder(); Product product = cb.BuildProduct(); Console.WriteLine(product.name); Console.Read(); } } //定义产品类 public class Product { public string name; } //定义建造者接口 public interface IBuilder { Product BuildProduct(); } //具体建造者 public class ConcreteBuilder : IBuilder { public Product BuildProduct() { Product product = new Product(); product.name = "DefaultName"; return product; } } }