//Client public BuilderPattern() { //随意替换具体Builder,不可用基类,见下注释 ConcreteBuilder2 builder = new ConcreteBuilder2(); //Director如有需要也可以切换 Director director = new Director(builder); director.Construct(); //具体构造器返回具体结果,可以将结果直接返回 //因可能会构造出不同的结果,因此不能将这个方法作为Builder基类,需客户端自行调用 builder.GetResult(); }
public void Run() { // Create director and builders Director director = new Director(); Builder b1 = new ConcreteBuilder1(); Builder b2 = new ConcreteBuilder2(); // Construct two products director.Construct(b1); Product p1 = b1.GetResult(); p1.Show(); director.Construct(b2); Product p2 = b2.GetResult(); p2.Show(); }
static void test() { // 客户找到电脑城老板说要买电脑,这里要装两台电脑 // 创建指挥者和构造者 Director director = new Director(); Builder b1 = new ConcreteBuilder1(); Builder b2 = new ConcreteBuilder2(); // 老板叫员工去组装第一台电脑 director.Construct(b1); // 组装完,组装人员搬来组装好的电脑 Computer computer1 = b1.GetComputer(); computer1.Show(); // 老板叫员工去组装第二台电脑 director.Construct(b2); Computer computer2 = b2.GetComputer(); computer2.Show(); Console.Read(); }