Esempio n. 1
0
        //Client
        public BuilderPattern()
        {
            //随意替换具体Builder,不可用基类,见下注释
            ConcreteBuilder2 builder = new ConcreteBuilder2();
            //Director如有需要也可以切换
            Director director = new Director(builder);

            director.Construct();

            //具体构造器返回具体结果,可以将结果直接返回
            //因可能会构造出不同的结果,因此不能将这个方法作为Builder基类,需客户端自行调用
            builder.GetResult();
        }
Esempio n. 2
0
        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();
        }