Creational Patterns - Builder Pattern (Make and return one object various ways)
When to use ?
Builder patten will divide complex in to simple steps. Its nothing but building complex object by simple objects. Its kind of building complex objects in to step by step simple objects.
Example :
Need to build specifications details of cars for specific brand . Lets take example as we need to build specification details for AudiS6 and AudiS7.
I will show in java example how to build car specifications
First need to create Car object this is contains specifications of a car product. It is a POJO object.
/**
*
* @author R.Amirtharaj
*/
public class Car {
public boolean isPowerSteering() {
return powerSteering;
}
public void setPowerSteering(boolean powerSteering) {
this.powerSteering = powerSteering;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
private double price;
private boolean powerSteering;
@Override
public String toString()
{
String output = "";
output += ("Price = "+price+"\n");
output += ("powerSteering = "+powerSteering+"\n");
return output;
}
}
Now we are going show Car Builder abstract builder class.
/**
*
* @author R.Amirtharaj
*/
public abstract class CarBuilder
{
protected Car car;
public Car getCar() {
return car;
}
public void createNewCar() {
car = new Car();
}
public abstract void setPowerSteering();
public abstract void setPrice();
}
The above java program show abstract builder class contains 2 abstract function which need to be implemented in derived class.
Below example shows how to create concrete class by extending car builder abstract class. We will create AudiS6 & AudiS7 concrete classes
which extends CarBuilder abstract builder class.
Concrete AudiS6 class
/**
*
* @author R.Amirtharaj
*/
public class AudiS6 extends CarBuilder
{
@Override
public void setPowerSteering() {
car.setPowerSteering(true);
}
@Override
public void setPrice() {
car.setPrice(2500000);
}
}
Concrete AudiS7 class
/**
*
* @author R.Amirtharaj
*/
public class AudiS7 extends CarBuilder
{
@Override
public void setPowerSteering() {
car.setPowerSteering(true);
}
@Override
public void setPrice() {
car.setPrice(3000000);
}
}
Now we all set ready by building small building blocks. Now we need to create Specifications director which will build the provided car
specifications.
Below is java code for Specifications director class
/**
*
* @author R.Amirtharj
*/
public class Specification {
private CarBuilder carBuilder;
public void setCarBuilder(CarBuilder aCarBuilder) {
carBuilder = aCarBuilder;
}
public Car getCar() {
return carBuilder.getCar();
}
public void constructSpecifications() {
carBuilder.createNewCar();
carBuilder.setPowerSteering();
carBuilder.setPrice();
}
}
Everything is created. Now we can create example of AudiS6 and AudiS7 car specification builder patten.
Below is example for creating specifications for AudiS6 and AudiS7
/**
*
* @author R.Amirtharaj
*/
public class Demo {
public static void main(String arg[])
{
Specification spec = new Specification();
CarBuilder audis6 = new AudiS6();
CarBuilder audis7 = new AudiS7();
// Specification for AudiS6
spec.setCarBuilder(audis6);
spec.constructSpecifications();
Car audis6_car = spec.getCar();
System.out.println("Specifiction for AudiS6");
System.out.println(audis6_car.toString());
// Specification for AudiS7
spec.setCarBuilder(audis7);
spec.constructSpecifications();
Car audis7_car = spec.getCar();
System.out.println("Specifiction for AudiS7");
System.out.println(audis7_car.toString());
}
}
Usage of builder patten in real time
* Build the complex objects by specifying there behavior and specifications . This object will be used as wrapper for constructing of building objects.
* It will avoid process of building complex objects form
No comments:
Post a Comment