·
Builder Pattern Creational Patterns (Java)
- When to use: If your application needs similar, but distinct, object types, then the builder pattern is a good choice.
- Builder pattern is used to construct a complex object step by step and the final step will return the object .
- This pattern allows a client object to construct a complex object by specifying only its type and content, being shielded from the details related to the object’s representation. This way the construction process can be used to create different representations. The logic of this process is isolated from the actual steps used in creating the complex object, so the process can be used again to create a different object form the same set of simple objects as the first one.
The builder design pattern delegates the construction of
an object to a builder class that, depending on the input, constructs an object
on your application’s behalf. The Builder Pattern consists of
a Builder, ConcreteBuilder, Director and Product.
- The Director object is responsible for the construction process of the complex object but delegates the actual creation and assembly to the Builder interface.
- The Builder object specifies the interface for creating parts of the complex object.
- The Product represents the complex object that is created by the ConcreteBuilder objects. The Product consists of multiple parts that are created separately by the ConcreteBuilder objects.
- The ConcreteBuilder objects create and assemble the parts that make up the Product through the Builder interface.
A client
object creates an instance of the Director object and passes it the appropriate
Builder object. The Director object invokes the methods on the Builder object
to create and initialize specific parts of the Product object. The Builder
receives content from the Director object and adds these to the Product object.
The Client object has a reference to the Builder object and retrieves the
created Product object from it.
Car.java
public class Car {
private
String engine;
private
String tires;
private
String exhaust;
public void setEngine( String engine ) {
this.engine
= engine;
}
public void setTires( String tires ) {
this.tires =
tires;
}
public void setExhaust( String exhaust ) {
this.exhaust
= exhaust;
}
}
CarBuilder.java
public abstract class CarBuilder {
protected
Car car;
public Car getCar() {
return car;
}
public void createNewCar() {
car = new
Car();
}
public
abstract void buildEngine();
public
abstract void buildTires();
public
abstract void buildExhaust();
}
FastAndFuriousCarBuilder.java
public class FastAndFuriousCarBuilder extends
CarBuilder {
public void
buildEngine() {
car.setEngine( "Twin Turbo with NOS"
);
}
public void
buildTires() {
car.setTires( "19 inch low profile racing
tires" );
}
public void buildExhaust() {
car.setExhaust(
"Deafening!" );
}
}
EconomyCarBuilder.java
public class EconomyCarBuilder extends CarBuilder {
public void
buildEngine() {
car.setEngine( "4 cylinder" );
}
public void buildTires() {
car.setTires( "15 inch with plenty of
air" );
}
public void buildExhaust() {
car.setExhaust( "Quiet and
efficient" );
}
}
Mechanic.java
public class Mechanic {
private
CarBuilder carBuilder;
public void
setCarBuilder( CarBuilder carBuilder ) {
this.carBuilder = carBuilder;
}
public Car getCar() {
return
carBuilder.getCar();
}
public void constructCar() {
carBuilder.createNewCar();
carBuilder.buildEngine();
carBuilder.buildTires();
carBuilder.buildExhaust();
}
}
CarExample.java
public class CarExample {
public
static void main( String[] args ) {
Mechanic
mechanic = new Mechanic();
CarBuilder
performanceBuilder = new FastAndFuriousCarBuilder();
CarBuilder
economyBuilder = new EconomyCarBuilder();
mechanic.setCarBuilder( performanceBuilder );
mechanic.constructCar();
Car porsche
= mechanic.getCar();
mechanic.setCarBuilder( economyBuilder );
mechanic.constructCar();
Car civic =
mechanic.getCar();
}
}
No comments:
Post a Comment