static void Main(string[] args) { // The Builder pattern is useful for creating complex objects that have multiple parts. The creation process of an object should // be independent of these parts; in other words the consturction process does not care how these parts are assembled. // You should be able to use the same construction process to create different representations of the objects Console.WriteLine("Builder Pattern"); Director director = new Director(); IBuilder buildOne = new Car("Dodge"); IBuilder buildTwo = new MotorCycle("Honda"); director.Construct(buildOne); Product p = buildOne.GetVehicle(); p.Show(); Console.WriteLine("----------------"); director.Construct(buildTwo); Product p2 = buildTwo.GetVehicle(); p2.Show(); Console.ReadLine(); }
static void Main(string[] args) { AbstractBuilder build_1 = new ConcreteBuilderA(); BuildDirector director_1 = new BuildDirector(build_1); Product p_1 = null; p_1 = director_1.ConstructProduct(); p_1.Show(); AbstractBuilder build_2 = new ConcreteBuilderB(); BuildDirector director_2 = new BuildDirector(build_2); p_1 = director_2.ConstructProduct(); p_1.Show(); Console.ReadLine(); }
static void Main(string[] args) { Director director = new Director(); Builder abuilder = new BuilderA(); Product product = director.Direct(abuilder); product.Show(); Console.ReadKey(); }
static void Main(string[] args) { Director director = new Director(); Builder builderA = new BuilderA(); Builder builderB = new BuilderB(); director.Construct(builderA); Product product1 = builderA.GetResult(); product1.Show(); director.Construct(builderB); Product product2 = builderB.GetResult(); product2.Show(); CharacterController characterController = new CharacterController(); ManHeroBuilder manHeroBuilder = new ManHeroBuilder(); FemanHeroBuilder femanHeroBuilder = new FemanHeroBuilder(); characterController.Construct(manHeroBuilder); Character character1 = manHeroBuilder.GetCharacter(); character1.Show(); characterController.Construct(femanHeroBuilder); Character character2 = femanHeroBuilder.GetCharacter(); character2.Show(); VideoPlayerController videoPlayerController = new VideoPlayerController(); VideoPlayerBuilder fullVideoPlayerBuilder = new VideoPlayerBuilder(); VideoPlayerBuilder SimplifiedVideoPlayerBuilder = new VideoPlayerBuilder(); VideoPlayerBuilder memoryVideoPlayerBuilder = new VideoPlayerBuilder(); videoPlayerController.FullMode(fullVideoPlayerBuilder); VideoPlayer videoPlayer1 = fullVideoPlayerBuilder.GetResult(); videoPlayer1.Show(); videoPlayerController.SimplifiedMode(SimplifiedVideoPlayerBuilder); VideoPlayer videoPlayer2 = SimplifiedVideoPlayerBuilder.GetResult(); videoPlayer2.Show(); videoPlayerController.MemoryMode(memoryVideoPlayerBuilder); VideoPlayer videoPlayer3 = memoryVideoPlayerBuilder.GetResult(); videoPlayer3.Show(); }
static void Main(string[] args) { Console.WriteLine("Builder Pattern implementation"); customCar = new Car("Suzuki Swift").StartUpOperations() .AddHeadlights(6) .InsertWheels() .BuildBody("Plastic") .EndOperations("Suzuki construction Completed!") .ConstructCar(); customCar.Show(); Console.ReadLine(); }
static void Main(string[] args) { Director d = new Director(); Builder b1 = new ConcreteBuilder1(); Builder b2 = new ConcreteBuilder2(); d.Construct(b1); Product p1 = b1.GetProduct(); p1.Show(); d.Construct(b2); Product p2 = b2.GetProduct(); p2.Show(); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("***Builder Pattern Demo***"); Director director = new Director(); IBuilder b1 = new Car("Ford"); IBuilder b2 = new MotorCycle("Honda"); // Making Car director.Construct(b1); Product p1 = b1.GetVehicle(); p1.Show(); //Making MotorCycle director.Construct(b2); Product p2 = b2.GetVehicle(); p2.Show(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***Builder Pattern Demo***"); Director director = new Director(); IBuilder car = new Car("Mercedes"); IBuilder motorCycle = new MotorCycle("Honda"); // Making Car director.Construct(car); Product p1 = car.GetVehicle(); p1.Show(); // Making Car director.Construct(motorCycle); Product p2 = motorCycle.GetVehicle(); p2.Show(); }
static void Main(string[] args) { Director director = new Director(); Builder b1 = new ConcreteBuilder1(); Builder b2 = new ConcreteBuilder2(); director.Construct(b1); // 指挥者用ConcreteBuilder1的方法来建造产品 Product p1 = b1.GetResult(); p1.Show(); director.Construct(b2); // 指挥者用ConcreteBuilder2的方法来建造产品 Product p2 = b2.GetResult(); p2.Show(); Console.Read(); }
private static void StructuralBuilder() { // 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(); // Wait for user Console.ReadKey(); }
static void Main(string[] args) { // 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(); //1.练习 VehicleBuilder builder; // Create shop with vehicle builders Shop shop = new Shop(); // Construct and display vehicles builder = new ScooterBuilder(); shop.Construct(builder); builder.Vehicle.Show(); builder = new CarBuilder(); shop.Construct(builder); builder.Vehicle.Show(); builder = new MotorCycleBuilder(); shop.Construct(builder); builder.Vehicle.Show(); Console.ReadKey(); }
static void Main(string[] args) { Director director = new Director(); Builder builderA = new ConcreteBuliderA(); Builder builderB = new ConcreteBuilderB(); //指挥者建造A director.Construct(builderA); Product productA = builderA.GetResult(); productA.Show(); //指挥者建造B director.Construct(builderB); Product productB = builderB.GetResult(); productB.Show(); //学生以及老师演示 Console.WriteLine("================================="); //实例化两个学生 Student studentA = new StudentA(); Student studentB = new StudentB(); //实例化老师 Teacher teacher = new Teacher(studentA); //指导A做实验 teacher.DirectExperiment(); teacher = new Teacher(studentB); teacher.DirectExperiment(); //老师为Director角色,学生为Builder角色,Teacher隔离了客户端与具体步骤的依赖 //在一些项目中,经常需要构建一些比较复杂的对象,并对其多个属性进行赋值的复杂操作,程序员的一些忽略可能导致某个属性未被赋值 //而引起对象的失效,在这种情况下使用构造者模式,创建一个Director来按部就班地指挥一个对象的创建,可以有效的避免意外的发生。 //使用创建者模式,用户只需要指定创建的类型就可以得到相应的对象,而具体的建造过程和细节就被Director和Builder隐藏了,这正是 //依赖倒转的体现:抽象不应该依赖于细节,细节应该依赖于抽象。 }
static void Main(string[] args) { Director director = new Director(); //声明一个精细构建1的类 ConcreteBuilder1 cb1 = new ConcreteBuilder1(); //通过统一的构造 director.Construct(cb1); //得到构造后的结果 Product p1 = cb1.GetResult(); //声明一个精细构建2的类 ConcreteBuilder2 cb2 = new ConcreteBuilder2(); //通过统一的构造 director.Construct(cb2); //得到构造后的结果 Product p2 = cb2.GetResult(); p1.Show(); p2.Show(); Console.ReadKey(); }