Builder Flashcards
builder pattern
used to build objects. Sometimes, the objects we create can be complex, made up of several sub-objects or require an elaborate construction process. The exercise of creating complex types can be simplified by using the builder pattern. A composite or an aggregate object is what a builder generally builds.
-builder pattern advantage
a builder pattern encapsulates or hides the process of building a complex object and separates the representation of the object and its construction. The separation allows us to construct different representations using the same construction process
The class diagram consists of
Builder
Concrete Builder
Director
Product
The builder pattern might seem similar to the abstract factory pattern but one difference is that
the builder pattern creates an object step by step whereas the abstract factory pattern returns the object in one go.
Builder class
contains a method for each component that can be part of the final product. }
These methods are selectively overridden by concrete builders depending on if the builders will be including that part in the final product variant that they are responsible for building.
Director class
contains The process or algorithm required to construct the product
Skipping the Director
You may find the builder pattern being used without the director. The client can directly instantiate the builder and invoke the required methods to get a product for itself.
The builder contains a method for each component that can be part of the final product. These methods are selectively overridden by concrete builders depending on if the builders will be including that part in the final product variant that they are responsible for building.
public abstract class AircraftBuilder {
public void buildEngine() {
}
public void buildWings() {
}
public void buildCockpit() {
}
public void buildBathrooms() {
}
abstract public IAircraft getResult();
}
implement two concrete builders, one for F-16 and one for Boeing-747
public class Boeing747Builder extends AircraftBuilder {
Boeing747 boeing747;
@Override
public void buildCockpit() {
}
@Override
public void buildEngine() {
}
@Override
public void buildBathrooms() {
}
@Override
public void buildWings() {
}
public IAircraft getResult() {
return boeing747;
}
}
public class F16Builder extends AircraftBuilder {
F16 f16;
@Override
public void buildEngine() {
// get F-16 an engine
// f16.engine = new F16Engine();
}
@Override
public void buildWings() {
// get F-16 wings
// f16.wings = new F16Wings();
}
@Override
public void buildCockpit() {
f16 = new F16();
// get F-16 a cockpit
// f16.cockpit = new F16Cockpit();
}
public IAircraft getResult() {
return f16;
}
}
The process or algorithm required to construct the aircraft which in our case is the specific order in which the different parts are created is captured by another class called the Director. The final product is still returned by the builders.
public class Director {
AircraftBuilder aircraftBuilder;
public Director(AircraftBuilder aircraftBuilder) {
this.aircraftBuilder = aircraftBuilder;
}
public void construct(boolean isPassenger) {
aircraftBuilder.buildCockpit();
aircraftBuilder.buildEngine();
aircraftBuilder.buildWings();
if (isPassenger)
aircraftBuilder.buildBathrooms();
}
}
The client will consume the pattern like so:
public class Client {
public void main() { F16Builder f16Builder = new F16Builder(); Director director = new Director(f16Builder); director.construct(false); IAircraft f16 = f16Builder.getResult(); } }