Builder Design Pattern

Introduction 

 
All right, folks, welcome to a new article on Builder Design Pattern.
 
A builder pattern should only be used for creating complex objects, like when object creation needs to be separated from its assembly, such as trees. I have explained that in my other blog Composite And Builder Design Pattern With A Tree. Feel free to visit it if you wish to learn that with a composite pattern.
 
Our recipe needs the following ingredients:
  1. Adding a builder interface
    This abstract base class defines all of the steps that must be taken in order to correctly create a product. For our example, interface IBuildMobile,

  2. Creating a concrete builder class
    These classes contain the functionality to create a particularly complex product. For our example, the class Apple & class OnePlus,

  3. Implementing a director
    The director-class controls the algorithm that generates the final product. A director object injects the product through the constructor. The director then calls methods of the concrete builder in the correct order to generate the product. On completion of the process, the GetProduct method of the builder object can be used to return the product. For our example, class Mobile & method which returns the mobile is GetMobile() in our example.
The builder pattern aims to separate the construction of a complex object from its representation so that the same construction process can create different representations.
Now you probably are scratching your head wondering what in the world was that?
 
Here is what I was trying to convey... The builder pattern removes any and all construction or initialization code from an object class and abstracts it out to an interface.
 
It is used to construct a complex object step-by-step and the final step will return the object of the final product.
 
Ok! Enough talk! Let's see how UML would look like for the example which we are going to design.
 
Let's have 2 mobile-phone's implementation: OnePlus & Apple, and have a client to only worry about calling these classes rather than worrying about what is there in mobile and how it supposed to get it.
 
Now imagine that you have to make the Mobile class's object. You need to have mobile's specifications, plus which mobile is what( OnePlus or Apple) & blah blah blah.
Why does the client have to worry about any of that?
 
Builder Design Pattern
 
Let's begin with interface IMobileRequirements. It defines all the requirements your mobile is supposed to have.
  1. public interface IMobileRequirements  
  2.    {  
  3.        void SetModelName(string modelName);  
  4.        void SetProcessor(string processor);  
  5.        void SetDisplayResolution(string resolution);  
  6.        void SetRAM(int RAM);  
  7.        void SetROM(int ROM);  
  8.    } 
Now it's concrete implementation: class MobileRequirements which sets all the basic requirements of mobile. 
  1. public class MobileRequirements : IMobileRequirements  
  2. {  
  3.     public string ModelName { getset; }  
  4.     public string Processor { getset; }  
  5.     public string DisplayResolution { getset; }  
  6.     public int RAM { getset; }  
  7.     public int ROM { getset; }  
  8.     public void SetDisplayResolution(string resolution)  
  9.     {  
  10.         this.DisplayResolution = resolution;  
  11.     }  
  12.   
  13.     public void SetModelName(string modelName)  
  14.     {  
  15.         this.ModelName = modelName;  
  16.     }  
  17.   
  18.     public void SetProcessor(string processor)  
  19.     {  
  20.         this.Processor = processor;  
  21.     }  
  22.   
  23.     public void SetRAM(int RAM)  
  24.     {  
  25.         this.RAM = RAM;  
  26.     }  
  27.   
  28.     public void SetROM(int ROM)  
  29.     {  
  30.         this.ROM = ROM;  
  31.     }  

Now our builder interface: IBuildMobile 
  1. public interface IBuildMobile  
  2.    {  
  3.        void BuildModelName();  
  4.        void BuildProcessor();  
  5.        void BuildDisplayResolution();  
  6.        void BuildRAM();  
  7.        void BuildROM();  
  8.        MobileRequirements GetMobileRequirements();  
  9.    } 
A concrete representation of  IBuildMobile. Basically our concrete builder classes.
 
class OnePlus
  1. namespace Builder_Design_Pattern  
  2. {  
  3.     public class OnePlus : IBuildMobile  
  4.     {  
  5.         public MobileRequirements specifications = new MobileRequirements();  
  6.         public void BuildDisplayResolution()  
  7.         {  
  8.             specifications.SetDisplayResolution("3168 * 1440");  
  9.         }  
  10.   
  11.         public void BuildProcessor()  
  12.         {  
  13.             specifications.SetProcessor("Qualcomm Snapdragon 865");  
  14.         }  
  15.   
  16.         public void BuildRAM()  
  17.         {  
  18.             specifications.SetRAM(8);  
  19.         }  
  20.   
  21.         public void BuildROM()  
  22.         {  
  23.             specifications.SetROM(128);  
  24.         }  
  25.   
  26.         public MobileRequirements GetMobileRequirements()  
  27.         {  
  28.             return this.specifications;  
  29.         }  
  30.   
  31.         public void BuildModelName()  
  32.         {  
  33.             specifications.SetModelName("One Plus 8 Pro");  
  34.         }  
  35.     }  

class Apple
  1. namespace Builder_Design_Pattern  
  2. {  
  3.     public class Apple : IBuildMobile  
  4.     {  
  5.         public MobileRequirements Specifications = new MobileRequirements();  
  6.   
  7.         public void BuildDisplayResolution()  
  8.         {  
  9.             Specifications.SetDisplayResolution("828 * 1792");  
  10.         }  
  11.   
  12.         public void BuildProcessor()  
  13.         {  
  14.             Specifications.SetProcessor("Apple A13 Bionic");  
  15.         }  
  16.   
  17.         public void BuildRAM()  
  18.         {  
  19.             Specifications.SetRAM(4);  
  20.         }  
  21.   
  22.         public void BuildROM()  
  23.         {  
  24.             Specifications.SetROM(128);  
  25.         }  
  26.   
  27.         public MobileRequirements GetMobileRequirements()  
  28.         {  
  29.             return this.Specifications;  
  30.         }  
  31.   
  32.         public void BuildModelName()  
  33.         {  
  34.             Specifications.SetModelName("iphone 11");  
  35.         }  
  36.     }  

Now our final director: class Mobile.
  1. public class Mobile  
  2.    {  
  3.        public IBuildMobile CellPhone;  
  4.        public Mobile(IBuildMobile cellPhone)  
  5.        {  
  6.            this.CellPhone = cellPhone;  
  7.        }  
  8.   
  9.        public MobileRequirements GetMobile()  
  10.        {  
  11.            return CellPhone.GetMobileRequirements();  
  12.        }  
  13.   
  14.        public void AssembleMobile()  
  15.        {  
  16.            this.CellPhone.BuildModelName();  
  17.            this.CellPhone.BuildDisplayResolution();  
  18.            this.CellPhone.BuildProcessor();  
  19.            this.CellPhone.BuildRAM();  
  20.            this.CellPhone.BuildROM();  
  21.        }  
  22.    } 
Last as usual, but not the least, the caller: class program. 
  1. class Program  
  2.   {  
  3.       static void Main(string[] args)  
  4.       {  
  5.           IBuildMobile Apple = new Apple();  
  6.           //IBuildMobile onePlus = new OnePlus();  
  7.           Mobile mobile = new Mobile(onePlus);  
  8.   
  9.           mobile.AssembleMobile();  
  10.           Console.WriteLine($" Model Name: {mobile.GetMobile().ModelName}\n Display resolution: {mobile.GetMobile().DisplayResolution} \n RAM: {mobile.GetMobile().RAM} GB\n ROM: {mobile.GetMobile().ROM} GB \n Processor: {mobile.GetMobile().Processor}");  
  11.   
  12.       }  
  13.   } 
Let's see the output when the client wants an Apple phone:
 
Builder Design Pattern
 
And when the client wants the Oneplus phone. Note: just uncomment above code and run the application:
 
Builder Design Pattern
 
Perfect & flawless!
 

Summary

 
In this article, we learned 
  1. Where the builder pattern fits into the larger design pattern picture.
  2. How to create an object class and abstract out its constructor into an interface.
  3. How to create and configure concrete builder representations.
  4. The role of the director-class and managing the overall build process.
I hope you've come away from this with a real grasp of the builder pattern and how it can be applied to your personal projects in the future.
 
If you've enjoyed the article, which I hope you have, please apply your knowledge to enliven the code in your projects.
 
if you have any questions or just want to connect, follow these links.
You can download the project for your references. It's free to use/modify & it's in .net core. 
As always, Happy coding!


Similar Articles