728x90 AdSpace

Sunday, 23 July 2017

Design Patterns - Builder Pattern

Builder Pattern

Definition

The Builder pattern can be used to ease the construction of a complex object from simple objects. The Builder pattern also separates the construction of a complex object from its representation so that the same construction process can be used to create another composition of objects. Related patterns include Abstract Factory and Composite.

Where to use

•When the algorithm for creating a complex object should be independent of the parts that make up the object and how they are assembled.
•When the construction process must allow different representations for the object that is constructed.
•When you want to insulate clients from the knowledge of the actual creation process and/or resulting product.

Benefits

•The built object is shielded from the details of its construction.
•Code for construction is isolated from code for representation and both are easy to replace without affecting the other.
•Gives you control over the construction process.
•Gives you the possibility to reuse and/or change the process and/or product independently.

Drawbacks/consequences

Need flexibility in creating various complex objects. Need to create complex, aggregate objects

Builder Pattern Class Diagram


In the class diagram above:
•The Builder specifies an abstract interface for creating parts of a Product.
•The ConcreteBuilder constructs and assembles parts of the product by implementing the Builder interface.
•The Director constructs an object using the Builder interface.
•The Product represents the object under construction.

Builder Pattern example

For instance to build a house, we will take several steps:
1. Build floor
2. Build walls
3. Build roof
Let's use an abstract class HouseBuilder to define these three steps. Any subclass of HouseBuilder will follow these three steps to build house (that is to say to implement these three methods in the subclass). Then we use a HouseDirector class to force the order of these three steps (that is to say that
we have to build walls after finished building floor and before building roof). The HouseClient orders the building of two houses, one wood house and one brick house. Even though the houses are of different types (wood and brick) they are built the same way, The construction process allows different representations for the object that is constructed.
public abstract class HouseBuilder {
protected House house;
protected Floor floor;
protected Walls walls;
protected Roof roof;
public abstract House createHouse();
public abstract Floor createFloor();
public abstract Walls createWalls();
public abstract Roof createRoof();
}
public class WoodBuilder extends HouseBuilder {
public Floor createFloor() {
floor = new WoodFloor();
return floor;
}
public House createHouse() {
house = new WoodHouse();
return house;
}
public Roof createRoof() {
roof = new WoodRoof();
return roof;
}
public Walls createWalls() {
walls = new WoodWalls();
return walls;
}
}
public class BrickBuilder extends HouseBuilder {
//similar to WoodBuilder
}
public class HouseDirector {
public House construcHouse(HouseBuilder builder) {
House house = builder.createHouse();
System.out.println(house.getRepresentation());
house.setFloor(builder.createFloor());
System.out.println(house.getFloor().getRepresentation());
house.setWalls(builder.createWalls());
System.out.println(house.getWalls().getRepresentation());
house.setRoof(builder.createRoof());
System.out.println(house.getRoof().getRepresentation());
return house;
}
}

public abstract class House {
protected Floor floor;
protected Walls walls;
protected Roof roof;
public Floor getFloor() {
return floor;
}
public void setFloor(Floor floor) {
this.floor = floor;
}
public Walls getWalls() {
return walls;
}
public void setWalls(Walls walls) {
this.walls = walls;
}
public Roof getRoof() {
return roof;
}
public void setRoof(Roof roof) {
this.roof = roof;
}
public abstract String getRepresentation();
}
public interface Floor {
public String getRepresentation();
}
public interface Walls {
public String getRepresentation();
}
public interface Roof {
public String getRepresentation();
}
public class WoodHouse extends House {
public String getRepresentation() {
return "Building a wood house";
}
}
public class WoodFloor implements Floor {
public String getRepresentation() {
return "Finished building wood floor";
}
}

public class WoodWalls implements Walls {
//similar to WoodFloor
}
public class WoodRoof implements Roof {
//similar to WoodFloor
}
// Similar structure for Brick family
public class BrickHouse extends House …
public class BrickFloor implements Floor …
public class BrickWalls implements Walls …
public class BrickRoof implements Roof …
public class HouseClient {
public static void main(String[] args) {
HouseDirector director = new HouseDirector();
HouseBuilder woodBuilder = new WoodBuilder();
BrickBuilder brickBuilder = new BrickBuilder();
// Build a wooden house
House woodHouse = director.construcHouse(woodBuilder);
System.out.println();
// Build a brick house
House brickHouse = director.construcHouse(brickBuilder);
}
}
When HouseClient is executed the result is:
c:>Building a wood house
c:>Finished building wood floor
c:>Finished building wood walls
c:>Finished building wooden roof
c:>
c:>Building a brick house
c:>Finished building brick floor
c:>Finished building brick walls
c:>Finished building brick roof

Usage example

Some examples of using the Builder pattern in knowledge engineering include different generators. Parsers in various compilers are also designed using the Builder pattern.

Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Builder Pattern Rating: 5 Reviewed By: Annamalai Thangaraj