public abstract class Builder { public abstract void BuildDoor(); public abstract void BuildWall(); public abstract void BuildWindow(); public abstract void BuildFloor(); public abstract void BuildHouseCeiling();
public class RomanHouseBuilder : Builder { public override void BuildDoor() { } public override void BuildFloor() {} public override void BuildHouseCeiling(){ } public override void BuildWall(){} public override void BuildWindow(){} public override House GetHouse() { return new RomanHouse(); } }
如果需要其他风格的访问,同可以创建相应的类继承House和HouseBuilder就可以。
客户端调用的代码如下
1 2 3 4 5 6 7
public class App { public static void Main() { House house = GameManager.CreateHouse(new RomanHouseBuilder()); } }
public class App { public static void Main() { string assemblyName = ConfigurationManager.AppSettings["AssemblyName"].Trim(); string className = ConfigurationManager.AppSettings["ClassName"].Trim(); Assembly assembly = Assembly.Load(assemblyName); Type type = assembly.GetType(className); Builder builder = Activator.CreateInstance(type) as Builder; House house = GameManager.CreateHouse(builder); } }