// "Handler"using System;using System.Collections.Generic;using System.Text;using System.Reflection;using System.Collections;namespace ConsoleApplication3{ abstract public class Builder { abstract public void BuilderWheel(); abstract public void BuilderEngine(); abstract public Product GetProduct(); } public class Product { Hashtable aHash = new Hashtable(); public Product() { aHash.Clear(); } public void add(string name,string value) { //if(!aHash.Contains(name)) aHash.Add(name, value); } public void show() { IDictionaryEnumerator myEnumerator = aHash.GetEnumerator(); while (myEnumerator.MoveNext()) { Console.WriteLine(myEnumerator.Value.ToString()); } } } public class CarBuilder : Builder { Product iProduct = new Product(); public CarBuilder() {} public override void BuilderEngine() { iProduct.add("Engine", "CarEngine"); } public override void BuilderWheel() { iProduct.add("Wheel", "CarWheel"); } public override Product GetProduct() { return iProduct; } } public class BusBuilder : Builder { Product iProduct = new Product(); public BusBuilder() { } public override void BuilderEngine() { iProduct.add("Engine", "BusEngine"); } public override void BuilderWheel() { iProduct.add("Wheel", "BusWheel"); } public override Product GetProduct() { return iProduct; } } public class Direct { public void Construct(Builder aBuilder) { aBuilder.BuilderEngine(); aBuilder.BuilderWheel(); } } class Program { public static void Main(string[] args) { string factoryName = System.Configuration.ConfigurationSettings.AppSettings["Builder"]; Builder aBuilder = (Builder)Assembly.GetExecutingAssembly().CreateInstance("ConsoleApplication3."+factoryName); Direct aDirect = new Direct(); aDirect.Construct(aBuilder); Product aProduct = aBuilder.GetProduct(); aProduct.show(); Console.ReadKey(); } }}
App.config配置: