GOF设计模式。NET 的 Creational Pattern 类别包括 Builder Design Pattern。它用于逐步构建复杂的对象。它提供了一个用于构建产品组件的工具。我想在这篇文章中解释什么是建筑模式以及它是如何工作的。
Builder 设计模式GOF 声称,Builder Design Pattern 使用循序渐进的方法和许多简单的组件来构建复杂的组件。为了能够使用相同的构造过程创建同一复杂项的多个表示形式,创建复杂对象的过程应该是通用的。
在 C# 中创建具有大量必填字段和可选字段的对象时,生成器设计模式会派上用场,尤其是在对象的开发过程错综复杂或项有多种潜在表示形式的情况下。目标是将复杂对象的表示与其生产分离,以便可以使用相同的构造过程生成不同的表示。
因此,将建筑过程与其描述分开是建筑商设计模式的主要目标。只有当对象的开发极其复杂时,才需要应用构建器设计模式。
构建器设计模式由四个元素组成,这些元素共同作用,将创建过程与其描述分离。下面列出了它们。
抽象构建器: 构建器包含一个界面,概述了创建有形输出所涉及的每个步骤。
混凝土建筑商: 所有抽象方法都由 Concrete Builder 类实现,这些类还实现了 Abstract Builder 接口。通过将 Builder 界面付诸实践,Concrete Builder 负责构建和组装产品的组件元素。此外,它还维护和定义它生成的表示形式。
导演: 主管使用构建者提供的不同程序确定产品的构建顺序。
产品: 产品是一个类,我们希望使用构建器设计模式来生成此类的对象。此类定义了用于制造最终产品的几个组件。
C# 实现代码创建产品以下是 Vehicle.cs 文件的代码。
public Vehicle { public string Model { get; set; } public string Engine { get; set; } public string Transmission { get; set; } public string Body { get; set; } public string FuelCapacity { get; set; } public string Mileage { get; set; } public string Displacement { get; set; } public List<string> Accessories { get; set; } public Vehicle() { Accessories = new List<string>(); } public void ShowInfo() { Console.WriteLine($"{nameof(Model)}: {Model}"); Console.WriteLine($"{nameof(Engine)}: {Engine}"); Console.WriteLine($"{nameof(Body)}: {Body}"); Console.WriteLine($"{nameof(Transmission)}: {Transmission}"); Console.WriteLine($"{nameof(FuelCapacity)}: {FuelCapacity}"); Console.WriteLine($"{nameof(Displacement)}: {Displacement}"); Console.WriteLine($"{nameof(Mileage)}: {Mileage}"); Console.WriteLine($"{nameof(Accessories)}:"); foreach (var accessory in Accessories) { Console.WriteLine("\t{0}", accessory); } } }创建抽象/接口生成器类以下是 IVehicleBuilder.cs 文件的代码。
public interface IVehicleBuilder{void SetModel();void SetEngine();void SetTransmission();void SetBody();void SetAccessories();void SetFuelCapacity();void SetDisplacement();void SetMileage();Vehicle GetVehicle();}
创建具体构建器类以下是 HeroSpendorBuilder.cs 文件的代码。
public HeroSplendorBuilder : IVehicleBuilder{private readonly Vehicle _vehicle;public HeroSplendorBuilder() { _vehicle = new Vehicle(); }public Vehicle GetVehicle() {return _vehicle; }public void SetAccessories() { _vehicle.Accessories.Add("Seat Cover"); _vehicle.Accessories.Add("Rear Mirror"); _vehicle.Accessories.Add("Handle cover"); _vehicle.Accessories.Add("Fuel cover"); }public void SetBody() { _vehicle.Body = "Commuter Bikes"; }public void SetDisplacement() { _vehicle.Displacement = "97.2 cc"; }public void SetEngine() { _vehicle.Engine = "4 Stroke"; }public void SetFuelCapacity() { _vehicle.FuelCapacity = "9.8 Ltrs"; }public void SetMileage() { _vehicle.Mileage = "80.6 Kmpl"; }public void SetModel() { _vehicle.Model = "Hero Splendor"; }public void SetTransmission() { _vehicle.Transmission = "120 km/hr"; }}以下是 HondaShineBuilder.cs 文件的代码。
public HondaShineBuilder : IVehicleBuilder{private readonly Vehicle _vehicle;public HondaShineBuilder() { _vehicle = new Vehicle(); }public Vehicle GetVehicle() {return _vehicle; }public void SetAccessories() { _vehicle.Accessories.Add("Seat Cover"); _vehicle.Accessories.Add("Rear Mirror"); _vehicle.Accessories.Add("Handle cover"); _vehicle.Accessories.Add("Fuel cover"); _vehicle.Accessories.Add("Helmet"); }public void SetBody() { _vehicle.Body = "Commuter Bikes"; }public void SetDisplacement() { _vehicle.Displacement = "123.94 cc"; }public void SetEngine() { _vehicle.Engine = "4 Stroke"; }public void SetFuelCapacity() { _vehicle.FuelCapacity = "10.5 Ltrs"; }public void SetMileage() { _vehicle.Mileage = "60 Kmpl"; }public void SetModel() { _vehicle.Model = "Honda Shine"; }public void SetTransmission() { _vehicle.Transmission = "140 km/hr"; }}创建控制器以下是 VehicleCreator.cs 文件的代码。
public VehicleCreator{private readonly IVehicleBuilder _vehicleBuilder;public VehicleCreator(IVehicleBuilder vehicleBuilder) { _vehicleBuilder = vehicleBuilder; }public void CreateVehicle() { _vehicleBuilder.SetModel(); _vehicleBuilder.SetEngine(); _vehicleBuilder.SetBody(); _vehicleBuilder.SetTransmission(); _vehicleBuilder.SetMileage(); _vehicleBuilder.SetFuelCapacity(); _vehicleBuilder.SetDisplacement(); _vehicleBuilder.SetAccessories(); }public Vehicle GetVehicle() {return _vehicleBuilder.GetVehicle(); }}客户端代码以下是 Program.cs 文件的代码。
var vehicleCreator = new VehicleCreator(new HeroSplendorBuilder()); vehicleCreator.CreateVehicle(); var vehicle = vehicleCreator.GetVehicle(); vehicle.ShowInfo(); Console.WriteLine(); vehicleCreator = new VehicleCreator(new HondaShineBuilder()); vehicleCreator.CreateVehicle(); vehicle = vehicleCreator.GetVehicle(); vehicle.ShowInfo();输出举例来说,假设您需要在构建器设计模式中添加更多的车辆构建器,那么这里我们将添加一个构建器,即 SuzukiGixxerBuilder。
以下是 SuzukiGixxerBuilder.cs 文件的代码。
public SuzukiGixxerBuilder : IVehicleBuilder{private readonly Vehicle _vehicle;public SuzukiGixxerBuilder() { _vehicle = new Vehicle(); }public Vehicle GetVehicle() {return _vehicle; }public void SetAccessories() { _vehicle.Accessories.Add("Seat Cover"); _vehicle.Accessories.Add("Rear Mirror"); _vehicle.Accessories.Add("Helmet"); }public void SetBody() { _vehicle.Body = "Sports Bikes"; }public void SetDisplacement() { _vehicle.Displacement = "155 cc"; }public void SetEngine() { _vehicle.Engine = "4 Stroke"; }public void SetFuelCapacity() { _vehicle.FuelCapacity = "12 Ltrs"; }public void SetMileage() { _vehicle.Mileage = "45 Kmpl"; }public void SetModel() { _vehicle.Model = "Suzuki Gixxer"; }public void SetTransmission() { _vehicle.Transmission = "180 km/hr"; }}以下是 Program.cs 文件的代码。
vehicleCreator = new VehicleCreator(new SuzukiGixxerBuilder()); vehicleCreator.CreateVehicle(); vehicle = vehicleCreator.GetVehicle(); vehicle.ShowInfo();输出如果你喜欢我的文章,请给我一个赞!谢谢