Intention/Definition: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.
Problem: in the face of complex object creation, objects are usually composed of certain algorithms, but each part of complex objects is often subject to drastic changes, the algorithms that combine them are relatively stable.
Solution: separately encapsulate the creation of each part
Participants and collaborators: Abstract builder classes that define an operation (Interface) for each component (part) that the Guide may require to create ). These operations do not do anything by default. A concretebuilder class is interested in building to redefine these operations. Each concretebuilder contains the code for creating all parts of a specific product, but the specific assembly process is defined in director.
Effect: the "assembly process" of complex objects is separated from the "component construction method", which can be changed independently.In simple builder mode, components of different product types cannot be combined or replaced..
Implementation:
Related mode [DP]:
Abstract Factory is similar to builder because it can create complex objects. The main difference is that the builder mode focuses on building a complex object step by step, while Abstract Factory focuses on product objects of multiple series (simple or complex ). Builder is the last step to return to the product.
Factory is the product that is returned immediately. Composite is usually generated by builder.
From the class diagram, we can see that the middle part is similar to the combination of the template method and Policy mode, which delays some operations to be executed in the subclass. However, the builder mode does not actually have any relationship with the template method, no algorithm step-related content is defined in the builder base class, and the assembly process is completed in the director class.
Example code: the following code takes the process of making a car or a motorcycle as an example. The manufacturing process can be abstracted as creating a car body, an engine, a wheel, or a door, however, the details of each step are different for both cars and motorcycles.
using System;using System.Collections.Generic;namespace Builder{ /// <summary> /// MainApp startup class for Real-World /// Builder Design Pattern. /// </summary> public class MainApp { /// <summary> /// Entry point into console application. /// </summary> public static void Main() { VehicleBuilder builder; // Create shop with vehicle builders Shop shop = new Shop(); builder = new CarBuilder(); shop.Construct(builder); builder.Vehicle.Show(); builder = new MotorCycleBuilder(); shop.Construct(builder); builder.Vehicle.Show(); // Wait for user Console.ReadKey(); } } /// <summary> /// The 'Director' class /// </summary> class Shop { // Builder uses a complex series of steps public void Construct(VehicleBuilder vehicleBuilder) { vehicleBuilder.BuildFrame(); vehicleBuilder.BuildEngine(); vehicleBuilder.BuildWheels(); vehicleBuilder.BuildDoors(); } } /// <summary> /// The 'Builder' abstract class /// </summary> abstract class VehicleBuilder { protected Vehicle vehicle; // Gets vehicle instance public Vehicle Vehicle { get { return vehicle; } } // Abstract build methods public abstract void BuildFrame(); public abstract void BuildEngine(); public abstract void BuildWheels(); public abstract void BuildDoors(); } /// <summary> /// The 'ConcreteBuilder1' class /// </summary> class MotorCycleBuilder : VehicleBuilder { public MotorCycleBuilder() { vehicle = new Vehicle("MotorCycle"); } public override void BuildFrame() { vehicle["frame"] = "MotorCycle Frame"; } public override void BuildEngine() { vehicle["engine"] = "500 cc"; } public override void BuildWheels() { vehicle["wheels"] = "2"; } public override void BuildDoors() { vehicle["doors"] = "0"; } } /// <summary> /// The 'ConcreteBuilder2' class /// </summary> class CarBuilder : VehicleBuilder { public CarBuilder() { vehicle = new Vehicle("Car"); } public override void BuildFrame() { vehicle["frame"] = "Car Frame"; } public override void BuildEngine() { vehicle["engine"] = "2500 cc"; } public override void BuildWheels() { vehicle["wheels"] = "4"; } public override void BuildDoors() { vehicle["doors"] = "4"; } } /// <summary> /// The 'Product' class /// </summary> class Vehicle { private string _vehicleType; private Dictionary<string, string> _parts = new Dictionary<string, string>(); // Constructor public Vehicle(string vehicleType) { this._vehicleType = vehicleType; } // Indexer public string this[string key] { get { return _parts[key]; } set { _parts[key] = value; } } public void Show() { Console.WriteLine("\n---------------------------"); Console.WriteLine("Vehicle Type: {0}", _vehicleType); Console.WriteLine(" Frame : {0}", _parts["frame"]); Console.WriteLine(" Engine : {0}", _parts["engine"]); Console.WriteLine(" #Wheels: {0}", _parts["wheels"]); Console.WriteLine(" #Doors : {0}", _parts["doors"]); } }}