Question

I am considering using the builder pattern in order to create complex objects within my application. However I have a concern over whether it is the correct pattern to use.

Take this example; here we see several concrete PizzaBuilders being created which works nicely. However, in my application I need the ability to pass in some information relating to what is being built. For example, in the case of the PizzaBuilders I would need the ability to create a custom pizza where the user can choose certain things such as the sauce and the toppings. It is worth noting that the information I would need to pass into the builders would vary.

Please could you advise whether I am looking at using the correct pattern for this problem or whether there may be something else better suited? If it is the correct pattern for the problem then please could you advise on the cleanest way to pass in the information to the builders? Would it be at the point of instantiation?

Était-ce utile?

La solution

It sounds like the builder pattern is what you need. In that wikipedia article we can see different predefined builders. However that's not the only way to use the Builder pattern.

You can certainly create custom objects with it and one of its strong points is that it allows you to create complex objects atomically without having to define multiple constructors, which leads to more maintanable code. Bear in mind that if there is not enough complexity to warrant use of this pattern it may be overkill.

Take a look at the description offered of this pattern in Joshua Bloch's Effective Java for a more comprehensive explanation. Here's an article about it

Autres conseils

For example, in the case of the PizzaBuilders I would need the ability to create a custom pizza where the user can choose certain things such as the sauce and the toppings

Can choose - these means optional elements. The Builder pattern would be perfect here. Unless you omit something in your question.

I would guess you could use something like:

// normal pizza:
Pizza normalPizza=PizzaBuilder.aPizza().withCheese().withSalami().build();

// custom pizza
Pizza customPizza=PizzaBuilder.aCustomPizza().withAnchovis().withHotSauce().withExtraCheese().withoutGarlic().build();

And what the builder would do internally is up to the implementation, as long as build() returns a Pizza object (or a suitable subclass). You can also do any consistency checking you need either in the with... methods or in the build().

If you need Typesafety for the results, you could use buildCustomPizza() to return your custom Pizza type.

Builder is not a grade A pattern, so it's tough to justify its usage sometime. And you might not find convincing reason to use it.

It's used if you want to make sure that your object construction is atomic. Consider Java way to construct a bean object using setter methods. In this case the problem, is that, if you miss to set one attribute then object will NOT be in a consistent state. And there is no way to figure about this unless you get some inconsistent application behavior.

So you should use Builder pattern when there are like 3 attributes and you want object creation to be atomic. I had used it some time back to create REST url like BASE_URL/param1/param2/param3.

So if your requirement is about creating/building an object and you have multiple attributes then you can go for this pattern.

Edit

Regarding implementation; i had followed the same approach as given on wikipedia.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top