Question

This one is straight from Effective java 2. I'm not sure what does this statement from Item 2 means

The Builder pattern is flexible. A single builder can be used to build multiple objects. The parameters of the builder can be tweaked between object creations to vary the objects.

I'm not able to come up with an example to do this. Please help me understand this with an example.

Was it helpful?

Solution

This blog post provides a good example of builder objects being used by the JavaFX 2 API.

A single builder can be used to build multiple objects.

The builder object is responsible for constructing a valid object but The object is not constructed until you call the build() method. This means the same builder can be used multiple times to construct completely different objects.

Example:

final TextBuilder builder = TextBuilder.create()

final Text text1 = builder
    .text("Hello World!")
    .x(50).y(50)
    .fill(Color.WHITE)
    .font(MY_DEFAULT_FONT)
    .build();

final Text text2 = builder
    .text("Goodbye World!")
    .x(50).y(100)
    .fill(Color.WHITE)
    .font(MY_DEFAULT_FONT)
    .build();

This can be done as many times as you want to create different objects. Just to re-iterate the point that the object is not created until the build() method is called, consider that you could do the following:

final Text text1 = TextBuilder.create()
    .text("Hello World!")
    .text("Goodbye World!")
    .text("Hello There!")
    .build();

which would result in the creation of one object, with the text set to 'Hello There' as this is the value of the property prior to the build() method being called.

The parameters of the builder can be tweaked between object creations to vary the objects.

The example below demonstrates this.

// Set the properties that are common to all objects.
final TextBuilder builder = TextBuilder.create()
    .x(50)
    .fill(Color.WHITE)
    .font(MY_DEFAULT_FONT);

// Use the builder to construct different objects that have the 
// properties set above as well as the additional ones set here.
final Text text1 = builder.text("Hello World!").y(50).build();
final Text text2 = builder.text("Goodbye World!").y(100).build();
final Text text3 = builder.text("JavaFX is fun!").y(150).build();

OTHER TIPS

if you want to construct a house you need: walls, floors, stairs, windows,... When you create a HouseBulder Class you can create all these objects in your HousBuilder Class. So the "user who want a house" do not have to know about the objects: walls, floors,.... and in which order the have to be constructed.

Builder pattern Builds a Complex object( a Complex object is combination of many objects and it builds each object one by one) and finally returns back the complex object.

The building of an object should be very generic."Micho" gave a very good example of house construction. you have to make the HouseBuilder class in such a way that any new HouseBuilder object can be different from the other HouseBuilder object. ie one person want to build his house with stairs inside his home, whereas other person wants his house with no stairs inside. So your complex object should be very generic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top