Design Pattern - Builder(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
Definition
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Participants
The classes and/or objects participating in this pattern are:
- Builder (VehicleBuilder)
- Specifies an abstract interface for creating parts of a Product object
- ConcreteBuilder (MotorCycleBuilder, CarBuilder, ScooterBuilder)
- Constructs and assembles parts of the product by implementing the Builder interface
- Defines and keeps track of the representation it creates
- Provides an interface for retrieving the product
- Constructs and assembles parts of the product by implementing the Builder interface
- Director (Shop)
- Constructs an object using the Builder interface
- Product (Vehicle)
- Represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled
- Includes classes that define the constituent parts, including interfaces for assembling the parts into the final result
- Represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled
Sample code in C#
This structural code demonstrates the Builder pattern in which complex objects are created in a step-by-step fashion. The construction process can create different object representations and provides a high level of control over the assembly of the objects.
// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Structural Builder Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{ using System; using System.Collections.Generic; /// <summary> /// Startup class for Structural Builder Design Pattern. /// </summary> public static class MainApp { #region Public Methods and Operators /// <summary> /// Entry point into console application. /// </summary> public static void Main() { // Create director and builders var 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(); } #endregion } /// <summary> /// The 'Director' class /// </summary> internal class Director { // Builder uses a complex series of steps #region Public Methods and Operators /// <summary> /// The construct. /// </summary> /// <param name="builder"> /// The builder. /// </param> public void Construct(Builder builder) { builder.BuildPartA(); builder.BuildPartB(); } #endregion } /// <summary> /// The 'Builder' abstract class /// </summary> internal abstract class Builder { #region Public Methods and Operators /// <summary> /// The build part a. /// </summary> public abstract void BuildPartA(); /// <summary> /// The build part b. /// </summary> public abstract void BuildPartB(); /// <summary> /// The get result. /// </summary> /// <returns> /// The <see cref="Product"/>. /// </returns> public abstract Product GetResult(); #endregion } /// <summary> /// The 'ConcreteBuilder1' class /// </summary> internal class ConcreteBuilder1 : Builder { #region Fields /// <summary> /// The product. /// </summary> private Product product = new Product(); #endregion #region Public Methods and Operators /// <summary> /// The build part a. /// </summary> public override void BuildPartA() { this.product.Add("PartA"); } /// <summary> /// The build part b. /// </summary> public override void BuildPartB() { this.product.Add("PartB"); } /// <summary> /// The get result. /// </summary> /// <returns> /// The <see cref="Product"/>. /// </returns> public override Product GetResult() { return this.product; } #endregion } /// <summary> /// The 'ConcreteBuilder2' class /// </summary> internal class ConcreteBuilder2 : Builder { #region Fields /// <summary> /// The product. /// </summary> private Product product = new Product(); #endregion #region Public Methods and Operators /// <summary> /// The build part a. /// </summary> public override void BuildPartA() { this.product.Add("PartX"); } /// <summary> /// The build part b. /// </summary> public override void BuildPartB() { this.product.Add("PartY"); } /// <summary> /// The get result. /// </summary> /// <returns> /// The <see cref="Product"/>. /// </returns> public override Product GetResult() { return this.product; } #endregion } /// <summary> /// The 'Product' class /// </summary> internal class Product { #region Fields /// <summary> /// The _parts. /// </summary> private List<string> parts = new List<string>(); #endregion #region Public Methods and Operators /// <summary> /// The add. /// </summary> /// <param name="part"> /// The part. /// </param> public void Add(string part) { this.parts.Add(part); } /// <summary> /// The show. /// </summary> public void Show() { Console.WriteLine("\nProduct Parts -------"); foreach (string part in this.parts) { Console.WriteLine(part); } } #endregion }}// Output:/*Product Parts -------PartAPartBProduct Parts -------PartXPartY*/
This real-world code demonstates the Builder pattern in which different vehicles are assembled in a step-by-step fashion. The Shop uses VehicleBuilders to construct a variety of Vehicles in a series of sequential steps.
// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Builder Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{ using System; using System.Collections.Generic; /// <summary> /// Startup class for Real-World Builder Design Pattern. /// </summary> public static class MainApp { #region Public Methods and Operators /// <summary> /// Entry point into console application. /// </summary> public static void Main() { VehicleBuilder builder; // Create shop with vehicle builders var 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(); } #endregion } /// <summary> /// The 'Director' class /// </summary> internal class Shop { // Builder uses a complex series of steps #region Public Methods and Operators /// <summary> /// The construct. /// </summary> /// <param name="vehicleBuilder"> /// The vehicle builder. /// </param> public void Construct(VehicleBuilder vehicleBuilder) { vehicleBuilder.BuildFrame(); vehicleBuilder.BuildEngine(); vehicleBuilder.BuildWheels(); vehicleBuilder.BuildDoors(); } #endregion } /// <summary> /// The 'Builder' abstract class /// </summary> internal abstract class VehicleBuilder { #region Fields /// <summary> /// The vehicle. /// </summary> protected Vehicle VehicleField; #endregion // Gets vehicle instance #region Public Properties /// <summary> /// Gets the vehicle. /// </summary> public Vehicle Vehicle { get { return this.VehicleField; } } #endregion #region Public Methods and Operators /// <summary> /// The build doors. /// </summary> public abstract void BuildDoors(); /// <summary> /// The build engine. /// </summary> public abstract void BuildEngine(); /// <summary> /// The build frame. /// </summary> public abstract void BuildFrame(); /// <summary> /// The build wheels. /// </summary> public abstract void BuildWheels(); #endregion } /// <summary> /// The 'ConcreteBuilder1' class /// </summary> internal class MotorCycleBuilder : VehicleBuilder { #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="MotorCycleBuilder"/> class. /// </summary> public MotorCycleBuilder() { this.VehicleField = new Vehicle("MotorCycle"); } #endregion #region Public Methods and Operators /// <summary> /// The build doors. /// </summary> public override void BuildDoors() { this.VehicleField["doors"] = "0"; } /// <summary> /// The build engine. /// </summary> public override void BuildEngine() { this.VehicleField["engine"] = "500 cc"; } /// <summary> /// The build frame. /// </summary> public override void BuildFrame() { this.VehicleField["frame"] = "MotorCycle Frame"; } /// <summary> /// The build wheels. /// </summary> public override void BuildWheels() { this.VehicleField["wheels"] = "2"; } #endregion } /// <summary> /// The 'ConcreteBuilder2' class /// </summary> internal class CarBuilder : VehicleBuilder { #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="CarBuilder"/> class. /// </summary> public CarBuilder() { this.VehicleField = new Vehicle("Car"); } #endregion #region Public Methods and Operators /// <summary> /// The build doors. /// </summary> public override void BuildDoors() { this.VehicleField["doors"] = "4"; } /// <summary> /// The build engine. /// </summary> public override void BuildEngine() { this.VehicleField["engine"] = "2500 cc"; } /// <summary> /// The build frame. /// </summary> public override void BuildFrame() { this.VehicleField["frame"] = "Car Frame"; } /// <summary> /// The build wheels. /// </summary> public override void BuildWheels() { this.VehicleField["wheels"] = "4"; } #endregion } /// <summary> /// The 'ConcreteBuilder3' class /// </summary> internal class ScooterBuilder : VehicleBuilder { #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="ScooterBuilder"/> class. /// </summary> public ScooterBuilder() { this.VehicleField = new Vehicle("Scooter"); } #endregion #region Public Methods and Operators /// <summary> /// The build doors. /// </summary> public override void BuildDoors() { this.VehicleField["doors"] = "0"; } /// <summary> /// The build engine. /// </summary> public override void BuildEngine() { this.VehicleField["engine"] = "50 cc"; } /// <summary> /// The build frame. /// </summary> public override void BuildFrame() { this.VehicleField["frame"] = "Scooter Frame"; } /// <summary> /// The build wheels. /// </summary> public override void BuildWheels() { this.VehicleField["wheels"] = "2"; } #endregion } /// <summary> /// The 'Product' class /// </summary> internal class Vehicle { #region Fields /// <summary> /// The parts. /// </summary> private Dictionary<string, string> parts = new Dictionary<string, string>(); /// <summary> /// The vehicle type. /// </summary> private string vehicleType; #endregion // Constructor #region Constructors and Destructors /// <summary> /// Initializes a new instance of the <see cref="Vehicle"/> class. /// </summary> /// <param name="vehicleType"> /// The vehicle type. /// </param> public Vehicle(string vehicleType) { this.vehicleType = vehicleType; } #endregion // Indexer #region Public Indexers /// <summary> /// The this. /// </summary> /// <param name="key"> /// The key. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public string this[
相關推薦
Design Pattern - Builder(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Observer(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Mediator(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Memento(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Iterator(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Interpreter(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Command (C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Flyweight(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Proxy(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Facade(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Decorator(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Composite(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Bridge(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Adapter(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Prototype(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Singleton(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Strategy(C )
Definition
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from cli
Design Pattern - Chain of Responsibility(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Factory Method(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
 
Design Pattern - Abstract Factory(C#)
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!