Design Patterns - Part 2 Builder Design Patterns

Hi,

Now I'm going to show you another Design pattern you can use for to software design problems you find again and again in real-world application development. This pattern is known as Builder Design Pattern

Definition

Separate the construction of a complex object from its representation so that the same construction process can create different representations. here is the sample code in c#

Main Class 

  /// <summary>  
   /// Main Class of the Application  
   /// </summary>  
   public class Program  
   {  
     /// <summary>  
     /// Main Method of the Program  
     /// </summary>  
     /// <param name="args"></param>  
     static void Main(string[] args)  
     {  
       // Create director and builders  
       var director = new Director();  
   
       Builder builderOne = new ConcreteBuilderOne();  
       Builder builderTwo = new ConcreteBuilderTwo();  
   
       // Construct two products  
       director.Construct(builderOne);  
       var productOne = builderOne.GetResult();  
       productOne.Show();  
   
       director.Construct(builderTwo);  
       var productTwo = builderTwo.GetResult();  
       productTwo.Show();  
   
       // Wait for user  
       Console.ReadLine();  
     }  
   }  

Director Class

  /// <summary>  
   /// The Director class  
   /// </summary>  
   public class Director  
   {  
     // Builder uses a complex series of steps  
     public void Construct(Builder builder)  
     {  
       builder.BuildPartOne();  
       builder.BuildPartTwo();  
     }  
   }  

Builder Classes
  /// <summary>  
   /// The Builder abstract class  
   /// </summary>  
   public abstract class Builder  
   {  
     public abstract void BuildPartOne();  
     public abstract void BuildPartTwo();  
     public abstract Product GetResult();  
   }  
   
   /// <summary>  
   /// The ConcreteBuilderOne class  
   /// </summary>  
   public class ConcreteBuilderOne : Builder  
   {  
     /// <summary>  
     /// Product Class Instance  
     /// </summary>  
     private readonly Product product = new Product();  
   
     public override void BuildPartOne()  
     {  
       this.product.Add("PartOne");  
     }  
   
     public override void BuildPartTwo()  
     {  
       this.product.Add("PartTwo");  
     }  
   
     /// <summary>  
     /// Get result of the Product  
     /// </summary>  
     /// <returns></returns>  
     public override Product GetResult()  
     {  
       return this.product;  
     }  
   }  
   
   /// <summary>  
   /// The ConcreteBuilderTwo class  
   /// </summary>  
   public class ConcreteBuilderTwo : Builder  
   {  
     /// <summary>  
     /// Instance of the Product Class  
     /// </summary>  
     private readonly Product product = new Product();  
   
     public override void BuildPartOne()  
     {  
       this.product.Add("Part1");  
     }  
   
     public override void BuildPartTwo()  
     {  
       this.product.Add("Part2");  
     }  
   
     /// <summary>  
     /// Get Results of the Product  
     /// </summary>  
     /// <returns>Instance of the Product</returns>  
     public override Product GetResult()  
     {  
       return this.product;  
     }  
   }  

Product Class
  /// <summary>  
   /// The Product class  
   /// </summary>  
   public class Product  
   {  
     /// <summary>  
     /// Collection of Parts  
     /// </summary>  
     private readonly List<string> parts = new List<string>();  
   
     public void Add(string part)  
     {  
       this.parts.Add(part);  
     }  
   
     public void Show()  
     {  
       Console.WriteLine("\nProduct Parts -------");  
       foreach (var part in this.parts)  
         Console.WriteLine(part);  
     }  
   }  

Output



Okay, that's Builder Design Pattern Implementation on C# if you want to know more about this design pattern please refer to this. from next post I'll show you how to Implement Factory Method. see you soon. Cheers.

Comments

Popular posts from this blog

Deep dive into OpenXML Part 1

Deep dive into OpenXML Part 2

Convert XML data to dynamic (ExpandoObject) Object