Builder Flashcards

1
Q

builder pattern

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

-builder pattern advantage

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The class diagram consists of

A

Builder
Concrete Builder
Director
Product

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The builder pattern might seem similar to the abstract factory pattern but one difference is that

A

the builder pattern creates an object step by step whereas the abstract factory pattern returns the object in one go.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Builder class

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Director class

A

contains The process or algorithm required to construct the product

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Skipping the Director

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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.

A

public abstract class AircraftBuilder {
public void buildEngine() {
}
public void buildWings() {
}
public void buildCockpit() {
}
public void buildBathrooms() {
}
abstract public IAircraft getResult();
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

implement two concrete builders, one for F-16 and one for Boeing-747

A

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;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

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();
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The client will consume the pattern like so:

A

public class Client {

public void main() {

    F16Builder f16Builder = new F16Builder();
    Director director = new Director(f16Builder);
    director.construct(false);

    IAircraft f16 = f16Builder.getResult();
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly