造者模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
建造者模式是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节。建造者模式属于对象创建型模式。根据中文翻译的不同,建造者模式又可以称为生成器模式。
建造者模式参与者
- Builder:是为创建一个产品对象的各个部件指定的抽象接口。
- ConcreteBuilder:具体建造者,实现Builder接口,构造和装配各个部件。
- Director:指挥者,是构建一个使用Builder接口的对象
- Product:具体产品
建造者模式基本代码
Product类——产品类由多个部件组成
namespace BuilderPattern.Structural { /// <summary> /// 产品类 /// </summary> class Product { IList<string> parts = new List<string>(); /// <summary> /// 添加产品部件 /// </summary> /// <param name="part"></param> public void Add(string part) { parts.Add(part); } public void Show() { Console.WriteLine(" 产品 创建----"); //列举所有产品部件 foreach (var part in parts) { Console.WriteLine(part); } } } }
Builder类——抽象建造者类,确定产品由两个部件PartA和PartB组成,并声明一个得到产品建造后结果的方法GetResult。
namespace BuilderPattern.Structural { abstract class Builder { public abstract void BuilderPartA(); public abstract void BuilderPartB(); public abstract Product GetResult(); } }
ConcreteBuilder1类——具体建造者类。
namespace BuilderPattern.Structural { class ConcreteBuilder1 : Builder { private Product product = new Product(); /// <summary> /// 构建部件A /// </summary> public override void BuilderPartA() { product.Add("ConcreteBuilder1 部件A"); } /// <summary> /// 构建部件B /// </summary> public override void BuilderPartB() { product.Add("ConcreteBuilder1 部件B"); } /// <summary> /// 获取产品 /// </summary> /// <returns></returns> public override Product GetResult() { return product; } } }
ConcreteBuilder2类——具体建造者类。
namespace BuilderPattern.Structural { class ConcreteBuilder2 : Builder { private Product product = new Product(); public override void BuilderPartA() { product.Add("部件X"); } public override void BuilderPartB() { product.Add("部件Y"); } public override Product GetResult() { return product; } } }
Director类——指挥者类。
namespace BuilderPattern.Structural { class Director { public void Construct(Builder builder) { builder.BuilderPartA(); builder.BuilderPartB(); } } }
客户端代码,客户不需要知道具体的建造过程。
static void Main(string[] args) { try { Director director = new Director(); Builder b1 = new ConcreteBuilder1(); Builder b2 = new ConcreteBuilder2(); director.Construct(b1); Product p1 = b1.GetResult(); p1.Show(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } }
结果如下:
用建造者模式实现交通工具的构造
交通工具类(Product类)代码如下:
namespace BuilderPattern.SituationSimulation { /// <summary> /// 交通工具类 /// </summary> class Vehicle { private Dictionary<string, string> _parts = new Dictionary<string, string>(); public string Name { get; set; } public Vehicle(string name) { this.Name = name; } /// <summary> /// 索引 /// </summary> /// <param name="key"></param> /// <returns></returns> public string this[string key] { get => _parts[key]; set => _parts[key] = value; } public void Show() { Console.WriteLine($" ------------{this.Name}组件---------------"); Console.WriteLine($" Frame : {_parts["frame"]}"); Console.WriteLine($" Engine : {_parts["engine"]}"); Console.WriteLine($" Wheels: { _parts["wheels"]}"); Console.WriteLine($" Doors : { _parts["doors"]}"); } } }
抽象交通工具部件建造类(Builder类)代码如下:
namespace BuilderPattern.SituationSimulation { /// <summary> /// 抽象交通工具部件建造类 /// </summary> abstract class VehicleBuilder { public Vehicle Vehicle { get; set; } /// <summary> /// 构造车架 /// </summary> public abstract void BuildFrame(); /// <summary> /// 构造改动机 /// </summary> public abstract void BuildEngine(); /// <summary> /// 构造车轮 /// </summary> public abstract void BuildWheels(); /// <summary> /// 构造车门 /// </summary> public abstract void BuildDoors(); } }
摩托车组件建造类(ConcreteBuilder)代码如下:
namespace BuilderPattern.SituationSimulation { /// <summary> /// 摩托车组件建造类 /// </summary> class MotorCycleBuilder : VehicleBuilder { public MotorCycleBuilder() { Vehicle = new Vehicle("MotorCycle"); } public override void BuildFrame() { Vehicle["frame"] = "MotorCycle Frame"; } public override void BuildEngine() { Vehicle["engine"] = "MotorCycle Engine 500 cc"; } public override void BuildWheels() { Vehicle["wheels"] = "MotorCycle has 2 Wheels"; } public override void BuildDoors() { Vehicle["doors"] = "MotorCycle has 0 Doors"; } } }
汽车组件建造类(ConcreteBuilder)代码如下:
namespace BuilderPattern.SituationSimulation { /// <summary> /// 汽车组件建造类 /// </summary> class CarCycleBuilder : VehicleBuilder { public CarCycleBuilder() { Vehicle = new Vehicle("Car"); } public override void BuildFrame() { Vehicle["frame"] = "Car Frame"; } public override void BuildEngine() { Vehicle["engine"] = "Car Engine 5000 cc"; } public override void BuildWheels() { Vehicle["wheels"] = "MotorCycle has 4 Wheels"; } public override void BuildDoors() { Vehicle["doors"] = "MotorCycle has 4 Doors"; } } }
商店类(Director类)代码如下:
namespace BuilderPattern.SituationSimulation { /// <summary> /// 商店(指挥者) /// </summary> class Shop { public void Construct(VehicleBuilder builder) { builder.BuildFrame(); builder.BuildEngine(); builder.BuildWheels(); builder.BuildDoors(); } } }
客户端代码:
static void Main(string[] args) { try { Shop shop = new Shop(); VehicleBuilder motorCycleBuilder = new MotorCycleBuilder(); shop.Construct(motorCycleBuilder); Vehicle motorCycle = motorCycleBuilder.Vehicle; motorCycle.Show(); VehicleBuilder carCycleBuilder = new CarCycleBuilder(); shop.Construct(carCycleBuilder); Vehicle car = motorCycleBuilder.Vehicle; car.Show(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); }
结果如下: