builder design pattern with realtime example

what is the builder design pattern in java?

this is one of the creational design patterns. creational patterns primarily deal with the creation of objects. we will explain in this post on builder design pattern with realtime example. the builder design pattern is used to create an object made from simple objects. it uses simple objects and step by step approach to create the object. bulder class is independent of other objects.

real time example for design pattern: creating builder-design-pattern-with-realtime-example.jpgwith different parts such as hard disk, processor, ram, graphic card. different customer preferers different configurations as per their needs.

A computer is an actual object which we want to build. smaller objects are hard disk, processor and ram, etc.

real time example in the JDK library is java.StringBuilder#append() .

it is used to solve the problems with factor method and abstract design patterns. these patterns will not work well when a class object contains a lot of attributes. when we use this factor and abstract factory design pattern, we will face these below problems,

  1. we are forced to send all parameters to class while creating the instance.
  2. parameters are not optional at least we should send null value.
  3. if we have many parameters, we need to remember the order of them while passing.

the build design pattern has come to rescue from these problems. it is the best to approach to create the object step by step.

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
// Example for Builder design pattern
public class BuilderdesignpatternDemo {

public static void main(String[] args) {
BuilderInterface highPCBuilder = new HighConfigPC();
Director dirObj = new Director();
dirObj.construct(highPCBuilder);
Computer computer= highPCBuilder.getComputer();
computer.show();
}

}

interface BuilderInterface { //Interface class
void buildBody();

void insertRAM();

void addHarddisk();

Computer getComputer();
}

class HighConfigPC implements BuilderInterface { //concrete class
private Computer computerObj = new Computer();

@Override
public void buildBody() {

computerObj.add("high quality body...");
}

@Override
public void insertRAM() {
computerObj.add("RAM 80GB.... :-)");

}

@Override
public void addHarddisk() {
computerObj.add("Harddisk 80TB.... :-)");

}

@Override
public Computer getComputer() {

return computerObj;
}

}

class LowConfigPC implements BuilderInterface {
private Computer computerObj = new Computer();

@Override
public void buildBody() {

computerObj.add("Low quality body...");
}

@Override
public void insertRAM() {
computerObj.add("RAM 2GB.... :-)");

}

@Override
public void addHarddisk() {
computerObj.add("Harddisk 100GB... :-)");

}

@Override
public Computer getComputer() {

return computerObj;
}

}

class Computer {
private LinkedList<String> parts;

public Computer() {

this.parts = new LinkedList<String>();
}

public void add(String part) {
parts.add(part);
}

public void show() {
for (int i = 0; i < parts.size(); i++) {
System.out.println(parts.get(i));
}
}

}
class Director // builder class which constructs the final object with different parts
{
BuilderInterface builder;
public void construct(BuilderInterface paramBuilder)
{
builder = paramBuilder;
builder.addHarddisk();
builder.buildBody();
builder.insertRAM();
}

}

expected output:

Harddisk 80TB…. 🙂
high quality body…
RAM 80GB…. 🙂

 

please run this program on your end to see how it works. thanks 🙂

 

 

 

 

 


Posted

in

by

Tags:

Comments

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started