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
- package com.bdp;
-
- public interface ICard
- {
- void addMagneticStrip();
- void addChip();
- Card getCard();
- }
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.
- ackage com.bdp;
-
- public class AbcPaymentCard implements ICard {
-
- private Card _card = new Card();
-
- public void addChip()
- {
- _card.add("AbcPaymentCard integrated with Chip Facility");
- }
-
- public void addMagneticStrip()
- {
- _card.add("AbcPaymentCard integrated with Magnetic-Strip Facility");
- }
-
- public Card getCard()
- {
- return _card;
- }
- }
- package com.bdp;
-
- public class XyzPaymentCard implements ICard{
-
- private Card _card = new Card();
-
- public void addChip()
- {
- _card.add("XyzPaymentCard integrated with Chip Facility");
- }
-
- public void addMagneticStrip()
- {
- _card.add("XyzPaymentCard integrated with Magnetic-Strip Facility");
- }
-
- public Card getCard()
- {
- return _card;
- }
- }
Below is the Card class, which is our product:
- package com.bdp;
-
- import java.util.ArrayList;
-
- public class Card {
- private ArrayList<String> _cards = new ArrayList<String>();
-
- public void add(String part)
- {
- _cards.add(part);
- }
-
- public void show()
- {
- System.out.println("--- Card Built ---");
- for (String part : _cards)
- System.out.println(part);
- System.out.println();
- }
- }
Below is the BankA Class, which got cards from Abc and Xyz payment Vendors and providing services to their customer
- package com.bdp;
-
- public class BankA {
-
- public void prepareCard(ICard card)
- {
- card.addMagneticStrip();
- card.addChip();
- }
- }
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
- package com.bdp;
-
- public class Customer {
-
- public static void main(String[] args) {
- BankA bank = new BankA();
-
- ICard cardTypeAbc = new AbcPaymentCard();
- bank.prepareCard(cardTypeAbc);
- Card cardAbc = cardTypeAbc.getCard();
- cardAbc.show();
-
- ICard cardTypeXyz = new XyzPaymentCard();
- bank.prepareCard(cardTypeXyz);
- Card cardXyz = cardTypeXyz.getCard();
- cardXyz.show();
- }
- }
Below is the output snap of the customer class:
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.