How do I use one builder to create multiple objects?

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

The Builder template is flexible. One builder can be used to build multiple objects. Builder parameters can be changed between objects to modify objects.

I cannot think of an example for this. Please help me understand this with an example.

+3


source to share


3 answers


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

One builder can be used to create multiple objects.

The builder object is responsible for constructing a valid object, but the Object is not created until you call the method build()

.
... This means that the same builder can be used multiple times to create 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. To iterate over the point that the object is not created until the method is called build()

, note that you can do the following:



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

      

which will result in a single object being created with the text set to "Hello There" as this is the property value before the invoked method is called build()

.

Builder parameters can be changed between object creation to modify 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();

      

+5


source


if you want to build the house you want: walls, floors, stairs, windows, ... When you create the HouseBulder class, you can create all these objects in your HousBuilder class. Thus, the "user who wants a house" does not need to know about objects: walls, floors, ... and in what order they should be built.



0


source


Drawing Builder Creates a complex object (a complex object is a combination of many objects and it builds each object every time) and finally returns a complex object.

The construction of the facility should be very general. Micho gave a very good example of building a house. you must make the HouseBuilder class so that any new HouseBuilder object can be different from another HouseBuilder object. those. one person wants to build his house with a staircase inside his house, while another person wants his house without a staircase inside. Therefore your complex object should be very generic.

0


source







All Articles