Sunday, August 17, 2014

Builder Pattern

Builder as the name suggests is about building objects. The objects are built in steps. It's one more higher level of abstraction than Abstract factory, where the steps to build the object and the object responsible for actual building are also separated out. For example let's say we want to make a builder for Car. Take real life example of let's say Ford. Ford has many vehicles in its portfolio. For example, let's take Fiesta and Mustang. Now there are two parts of this problem. One is the building of the Mustang or Fiesta and the second is the plant where the cars are actually build. So we have to handle with both steps of building and actual building. If Ford would have only one factory to build all the cars, we could have used Abstract factory. If Ford would have only one factory and one car, we could have used Factory. Also in builder pattern, we can use abstract factory also for building different cars. So a lot of mix and match is possible as per the requirement.

Coming back to our present problem, Let's define the Car

public class Car {

private String chasis;
private String interior;
private String engine;

public void setChasis(String chasis) {
this.chasis = chasis;
}

public void setInterior(String interior) {
this.interior = interior;
}

public void setEngine(String engine) {
this.engine = engine;
}

}

Now let's make the builder

public abstract class CarBuilder {

protected Car car;

public void createCar() {
this.car = new Car();
}

public Car getcar() {
return car;
}

public abstract void buildChasis();

public abstract void buildInterior();

public abstract void buildEngine();
}

And the actual builders

public class FiestaCarBuilder extends CarBuilder {

@Override
public void buildChasis() {
car.setChasis("Small Chasis");
}

@Override
public void buildInterior() {
car.setInterior("Simple but elegant");
}

@Override
public void buildEngine() {
car.setEngine("Small engine");
}

}

and another concrete builder

public class MustangCarBuilder extends CarBuilder {

@Override
public void buildChasis() {
car.setChasis("A big chasis");
}

@Override
public void buildInterior() {
car.setInterior("Luxurious");
}

@Override
public void buildEngine() {
car.setEngine("Powerful");
}

}

Now we need to represent the actual plant for making cars

public class FordManufacturingPlant {

private CarBuilder carBuilder;

public void setCarBuilder(CarBuilder carBuilder) {
this.carBuilder = carBuilder;
}

public void constuctCar() {
carBuilder.createCar();

carBuilder.buildChasis();
carBuilder.buildEngine();
carBuilder.buildInterior();
}

public Car deliverCar() {
return carBuilder.getcar();
}
}

And finally the main app

public class Builder {

public static void main(String[] args) {
FordManufacturingPlant plant = new FordManufacturingPlant();
FiestaCarBuilder fiestaCarbuilder = new FiestaCarBuilder();
MustangCarBuilder mustangCarbuilder = new MustangCarBuilder();

// Let's build 100 fiesta cars
List<Car> fiestCarList = new ArrayList<Car>();
for (int i = 0; i < 100; i++) {
plant.setCarBuilder(fiestaCarbuilder);
plant.constuctCar();
fiestCarList.add(plant.deliverCar());
}

// Let's build 10 mustangs
List<Car> mustangCarList = new ArrayList<Car>();
for (int i = 0; i < 10; i++) {
plant.setCarBuilder(mustangCarbuilder);
plant.constuctCar();
mustangCarList.add(plant.deliverCar());
}
}

No comments:

Post a Comment