Builder Design Pattern

 Builder Pattern says that "construct a complex object from simple objects using step-by-step approach"

It is mostly used when object can't be created in single step like in the de-serialization of a complex object.


Main Advantages of Builder Design Pattern:-

  • It provides clear separation between the construction and representation of an object.
  • It provides better control over construction process.
  • It supports to change the internal representation of objects.

                         UML Diagram CD Packaging Company:- 

Now will directly dive into the implementation:-

1) Create Packing interface

File: Packing.java
  1. public interface Packing {  
  2.             public String pack();  
  3.             public int price();  
  4. }  

2) Create 2 abstract classes CD and Company

Create an abstract class CD which will implement Packing interface.

File: CD.java
  1. public abstract class CD implements Packing{  
  2. public abstract String pack();  
  3. }  
File: Company.java
  1. public abstract class Company extends CD{  
  2.    public abstract int price();  
  3. }  

3) Create 2 implementation classes of Company: Sony and Samsung

File: Sony.java
  1. public class Sony extends Company{  
  2.     @Override  
  3.         public int price(){   
  4.                         return 20;  
  5.       }  
  6.     @Override  
  7.     public String pack(){  
  8.              return "Sony CD";  
  9.         }         
  10. }//End of the Sony class.  
File: Samsung.java
  1. public class Samsung extends Company {  
  2.     @Override  
  3.         public int price(){   
  4.                         return 15;  
  5.     }  
  6.     @Override  
  7.     public String pack(){  
  8.              return "Samsung CD";  
  9.         }         
  10. }//End of the Samsung class.  

4) Create the CDType class

File: CDType.java
  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3. public class CDType {  
  4.              private List<Packing> items=new ArrayList<Packing>();  
  5.              public void addItem(Packing packs) {    
  6.                     items.add(packs);  
  7.              }  
  8.              public void getCost(){  
  9.               for (Packing packs : items) {  
  10.                         packs.price();  
  11.               }   
  12.              }  
  13.              public void showItems(){  
  14.               for (Packing packing : items){  
  15.              System.out.print("CD name : "+packing.pack());  
  16.              System.out.println(", Price : "+packing.price());  
  17.           }       
  18.             }     
  19. }//End of the CDType class.  

5) Create the CDBuilder class

File: CDBuilder.java
  1. public class CDBuilder {  
  2.                   public CDType buildSonyCD(){   
  3.                      CDType cds=new CDType();  
  4.                      cds.addItem(new Sony());  
  5.                      return cds;  
  6.               }  
  7.               public CDType buildSamsungCD(){  
  8.              CDType cds=new CDType();  
  9.              cds.addItem(new Samsung());  
  10.              return cds;  
  11.               }  
  12. }// End of the CDBuilder class.  

6) Create the BuilderDemo class

File: BuilderDemo.java
  1. public class BuilderDemo{  
  2.  public static void main(String args[]){  
  3.    CDBuilder cdBuilder=new CDBuilder();  
  4.    CDType cdType1=cdBuilder.buildSonyCD();  
  5.    cdType1.showItems();  
  6.   
  7.    CDType cdType2=cdBuilder.buildSamsungCD();  
  8.    cdType2.showItems();  
  9.  }  
  10. }  
So As you see the code implementation was lengthy and complex but of worth.
As you saw we used CD company example how we do the packing 
and calculate the CD Price using CDBuilder class.



 

Comments

Popular posts from this blog

3 Lines of Code And You Can Download Your Favourite Youtube Video.

Leetcode Problem(Easy) Roman to Integer

Leetcode : (Find Minimum in Rotated Sorted Array)