Introduction to Builder Pattern.

In this article we are going to learn about Builder Design Pattern. What is Builder design pattern? What are the basic principles? How to implement and where we can use this.
I have used Java with Eclipse and C# with VS2010.

What is Builder design pattern?

In OO design, some time we have to deal with very complex object creation or situation where many objects needed to be created in complex manners or even steps.
Let say a , in a banking money transfer scenario, the main transaction processing object need to perform many complex steps and need initiate several sensitive as well as complex objects. So, it will be very hard to keep every thing managed for a client sequentially. In those case we can use Builder pattern.
Builder pattern allows a client to contract object by specifying only its type and content. Client will never know about the inner details related to created object. So, the system can abstract/isolate the process of creating object from the client.

In this manner, a client will get a generic way to create an object whiteout knowing actual process of creating the object. We can re use the process to create another similar object for that client.
Builder pattern uses factory building mechanism to decide which class to initiate in order to build desired type of objects.

Let' see some example. Suppose, we are creating a an customer bank account. The customer account making process same for a client who actually create an account in the bank. But, in background to create account, we need NID verification, residence verification, register in account list, categorize the customer based on credit, setting activity rules for a certain period of time, setting banking policy for the customer. So, after making so many objects in the process, end product is a successfully created bank account which is client main interest.

Another example, In the hospital management system. When a new patient comes, the patient need to only how he can get service(consider an object responsible for that) , but in the background we have to create a bed/room for him, assign a doctor and nurse, admit in an appropriate section, scheduling the treatment, arrange access to medicine store, keeping schedule for visiting, arranging food schedules etc. So, we may apply builder pattern here. 

Or , even a car/vehicle factory where we need to make the car/vehicle through complex process but client need to get the car in simple manner. 

Basic Principle :
- An interface will define object creation
- Sub class or implemented class will decide which class's instance to make
- Use Interface to refer newly created object


How to implement?

Let's think about how to implement. Okay, first of all we need a Product object which we will be build. We may also use interface for making similar category products, in the example, I have used that.

And, now product specific builder. Who actually know product very well and can build product part by part. The builder should have ever function to build a product.

And now we need a Director. I have renamed as maker who makes the product. He has the steps which are defined in a builder. That means , a director will contain a builder. So, here is the factory trick. If we use a builder interface for a maker, maker will be generic and we can use same maker for more than one type builder. So, the maker(Director) will build the product by builder interface and get a product from the builder. So, the builder interface should have the methods not only for building product but also getting the product out from builder

And, finally a client. In here client is kind of separate form building process, so it will initiate a builder by interface and give responsibility to Director(maker) to build the product. And, it will get the product. That should be end of builder objectives. Let see the example to get more details ideas.

Example : Let's assume a problem domain. Think about a Mobile factory. A client gives order to a mobile engineer(we will call him mobile maker who will be acting as director). And the maker will have a builder. He instruct the builder to build product and get the product to client. He does not know how the builder builds mobile, but he provides instruction to every steps needed for building.
And our builder builds mobile parts by parts and when complete, he can send a mobile back to maker if maker ask. For easy example, we have kept 5 component of a mobile. I have kept them as string field, but in complex scenario. this product can contains other custom class(or even products).
So, Lets define our product Mobile. For standard practice, I have applied an IMobile interface which I have implemented as Mobile class. So, here is the interface contains basic items of a mobile.
public interface IMobile {
    public void setKeypad(String keypad);
    public void setSpeaker(String speaker) ;    
    public void setDisplayUnit(String displayUnit) ;
    public void setMic(String mic) ;
    public void setNetwrkRadio(String netwrkRadio) ;

And Implementing Mobile class.

public class Mobile implements IMobile{
    private String displayUnit;
    private String keypad;
    private String speaker;
    private String mic;
    private String netwrkRadio;
    public String getDisplayUnit() {
        return displayUnit;    
    public String getKeypad() {
        return keypad;
    public String getNetwrkRadio() {
        return netwrkRadio;
    public String getSpeaker() {
        return speaker;
    public String getMic() {
        return mic;
    public void setKeypad(String keypad) {
        this.keypad = keypad;
    public void setSpeaker(String speaker) {
        this.speaker = speaker;
    public void setDisplayUnit(String displayUnit) {
        this.displayUnit = displayUnit;
    public void setMic(String mic) {
        this.mic = mic;
    public void setNetwrkRadio(String netwrkRadio) {
        this.netwrkRadio = netwrkRadio;

And, we need a builder interface that actually tells the steps to build a mobile as well as get a builded mobile.

public interface IMobileBuilder {
    public void buildKeypad();
    public void buildSpeaker();
    public void buildDisplayUnit() ;
    public void buildMic();
    public void buildNetwrkRadio();
    public Mobile getMobile();

And implemented builder class who actually have the definition of every step of building a mobile.

public class MobileBuilder implements IMobileBuilder{
    private Mobile aMobile=null;
    public MobileBuilder() {
    aMobile=new Mobile();
    public void buildKeypad(){
    public void buildSpeaker(){
        aMobile.setSpeaker("Loude Speakers ");
    public void buildDisplayUnit(){
        aMobile.setDisplayUnit("Touch Display");
    public void buildMic(){
        aMobile.setMic("High sensored mike");
    public void buildNetwrkRadio(){
        aMobile.setNetwrkRadio("3g Network");
    public Mobile getMobile() {
        return aMobile;
Now, we have builder class and product class. Now we need a Director class who actually direct how to builder will make a mobile. I have used as maker (to understand about the responsibility that who actually instruct ti build mobile) . So MobileMaker class who contains a builder and instruct him to build mobile.

public class MobileMaker {
    private IMobileBuilder myBuilder;
    public MobileMaker(IMobileBuilder aBuilder){
    public Mobile getMobile(){
        return myBuilder.getMobile();
    public void buildMobile(){

So, this is very important. This director makes client fully abstract about mobile's making process and inside stuffs. This is the main idea of  Builder pattern. We can keep building process fully abstracted.

Now, we need a client, who actually calls the director (with a builder ) and ask a mobile from the director. So, in our case, program class will contain main and MobileMaker will return an mobile as product.

public class Program {
    public static void main(String[] args) {
        IMobileBuilder aBuilder = new MobileBuilder();
        MobileMaker aMaker = new MobileMaker(aBuilder);
        Mobile aMobile = aMaker.getMobile();
        System.out.println("A Mobile is ordered to build");
        System.out.println("The Mobile Keypad = "+aMobile.getKeypad());
        System.out.println("The Mobile Display = "+aMobile.getDisplayUnit());
        System.out.println("The Mobile Netwrk = "+aMobile.getNetwrkRadio());
        System.out.println("The Mobile Speaker = "+aMobile.getSpeaker());
        System.out.println("The Mobile Mic = "+aMobile.getMic());

Java Example : I have use some extra console print to understand

C# Example :
1. I have make most of the method as property (standard dot net way)
2. Some methods are renamed and change due to dotnet mechanism. 

Uses :
1. When we want to keep a complex algorithm to create objects independent/separate from the object who will actually use it. 
2. When system needs to provide simple representation of build objects.
3. Very useful for re engineering legacy system that uses complex object creating process
4. Very useful to design a secure system where permission and security of each layer is important and client need not be informed about the process of object creation process.


No comments:

Post a Comment