static void Main(string[] args) { try { //使用重载构造函数或可选参数构造函数实现简单的建造者模式 Computer computer = new Computer("Intel", "DDR", 5); Console.WriteLine(computer); //使用链式调用实现建造者模式;链式调用顺序可变,灵活调用 ComputerBuilder computerBuilder = new ComputerBuilder.Builder("因特尔", "三星") .setDisplay("三星24寸") .setKeyboard("罗技") .setUsbCount(2) .build(); Console.WriteLine(computerBuilder); //使用builder模式 Console.WriteLine("**********************************"); { AbstractBuilder builder = new BuilderBYD(); Director director = new Director(builder); director.GetCar(); } } catch (Exception) { throw; } Console.ReadKey(); }
static void Main(string[] args) { try { Console.WriteLine("欢迎来到.net高级班公开课之设计模式特训,今天是Eleven老师为大家带来的建造者模式Builder"); Console.WriteLine("**********************************"); { AbstractBuilder builder = new BuilderFord(); builder.Engine(); builder.Wheels(); builder.Light(); builder.Car(); } Console.WriteLine("**********************************"); { AbstractBuilder builder = new BuilderBYD(); builder.Engine(); builder.Wheels(); builder.Light(); builder.Car(); } Console.WriteLine("**********************************"); { AbstractBuilder builder = new BuilderBYD(); Director director = new Director(builder); director.GetCar(); } Console.WriteLine("**********************************"); { AbstractBuilder builder = new BuilderVolkswagen(); Director director = new Director(builder); director.GetCar(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }
static void Main(string[] args) { { AbstractBuilder ford = new BuilderFord(); ford.Engine(); ford.Wheels(); ford.Light(); ford.Car(); AbstractBuilder bmw = new BuilderBMW(); bmw.Engine(); bmw.Wheels(); bmw.Light(); bmw.Car(); } Console.WriteLine("************************升级**************************"); { AbstractBuilder ford2 = new BuilderFord(); Director dir = new Director(ford2); dir.GetCar(); } Console.Read(); }