Builder Design Pattern Using a Java Sample

Introduction

Builder Design pattern is a creational design pattern. During the construction of complex objects, the Builder design pattern plays a key role.

Where to use Builder Design Pattern?

When we have to build a complex object, (i.e. a single class contains multiple complex child objects), in order to create a main class object, we need to build it's complex child objects. In this case, a Builder Design Pattern plays a key role in building complex objects.
 
Why use it? 
 
While creating multiple objects to accomplish the main class object, we come across one common function, the building object. In this case, the builder design pattern suggests defining a common class that is responsible only for building the required object.
 
Players in this pattern are:
  • Builder – one who will define contract to build different parts of one product
  • ConcreteBuilder – one who will implements builder contract
  • Product – This is a complex product with contains conation several subparts.
  • ProductServer – the one who use the Product and provide its services to whom it is required
We will see now how to implement it with an example.
 
Problem Definition
 
Build an application for payment card vendors that have to prepare Magnetic strip and Chip based card so that card venders can provide magnetic strip and chip-based services to a bank and a bank can provide services to customers.
 
For the above problem, we will use the builder design pattern. Below are the players:
  • Builder – ICard
  • ConcreteBuilder – AbcPaymentCard, XyzPaymentCard
  • Product – Card
  • ProductServer – BankA
Below is the ICard Contract which will define what objects/Part need to build
  1. package com.bdp;  
  2.   
  3. public interface ICard  
  4. {  
  5.     void addMagneticStrip();  
  6.     void addChip();  
  7.     Card getCard();  
  8. }  
Below is AbcPaymentCard, XyzPaymentCard which implements contract defined by the Builder which is ICard in our case. Here we can say that Abc and Xyz are responsible for building a card with Magnetic strip and Chip abilities. 
  1. ackage com.bdp;  
  2.   
  3. public class AbcPaymentCard implements ICard {  
  4.   
  5.     private Card _card = new Card();  
  6.   
  7.     public void addChip()  
  8.     {  
  9.         _card.add("AbcPaymentCard integrated with Chip Facility");  
  10.     }  
  11.   
  12.     public void addMagneticStrip()  
  13.     {  
  14.         _card.add("AbcPaymentCard integrated with Magnetic-Strip Facility");  
  15.     }  
  16.   
  17.     public Card getCard()  
  18.     {  
  19.         return _card;  
  20.     }  
  21. }  
  1. package com.bdp;  
  2.   
  3. public class XyzPaymentCard implements  ICard{  
  4.   
  5.     private Card _card = new Card();  
  6.   
  7.     public void addChip()  
  8.     {  
  9.         _card.add("XyzPaymentCard integrated with Chip Facility");  
  10.     }  
  11.   
  12.     public void addMagneticStrip()  
  13.     {  
  14.         _card.add("XyzPaymentCard integrated with Magnetic-Strip Facility");  
  15.     }  
  16.   
  17.     public Card getCard()  
  18.     {  
  19.         return _card;  
  20.     }  
  21. }  
Below is the Card class, which is our product:
  1. package com.bdp;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. public class Card {  
  6.     private ArrayList<String> _cards = new ArrayList<String>();  
  7.   
  8.     public void add(String part)  
  9.     {  
  10.         _cards.add(part);  
  11.     }  
  12.   
  13.     public void show()  
  14.     {  
  15.         System.out.println("--- Card Built ---");  
  16.         for (String part : _cards)  
  17.                 System.out.println(part);  
  18.         System.out.println();  
  19.     }  
  20. }  
Below is the BankA Class, which got cards from Abc and Xyz payment Vendors and providing services to their customer 
  1. package com.bdp;  
  2.   
  3. public class BankA {  
  4.   
  5.     public void prepareCard(ICard card)  
  6.     {  
  7.         card.addMagneticStrip();  
  8.         card.addChip();  
  9.     }  
  10. }  
Below is the customer class, which will communicate to bank for card. In general, we can say that the customer is visiting the bank for payment cards
  1. package com.bdp;  
  2.   
  3. public class Customer {  
  4.   
  5.     public static void main(String[] args) {  
  6.         BankA bank = new BankA();  
  7.   
  8.         ICard cardTypeAbc = new AbcPaymentCard();  
  9.         bank.prepareCard(cardTypeAbc);  
  10.         Card cardAbc = cardTypeAbc.getCard();  
  11.         cardAbc.show();  
  12.   
  13.         ICard cardTypeXyz = new XyzPaymentCard();  
  14.         bank.prepareCard(cardTypeXyz);  
  15.         Card cardXyz = cardTypeXyz.getCard();  
  16.         cardXyz.show();  
  17.     }  
  18. }  
Below is the output snap of the customer class:
 
Builder Design Pattern Using Java Sample
 

Summary

In this article, we learned about the Builder design pattern and how we can use it. This pattern is really useful when we need to implement complex objects.