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;
- }
- }


Join the conversation! Your thoughts help the community grow.