From When would you use the Builder Pattern?,
It is said that builder pattern is appropriate for Pizza example.
Why not Decorator ? by treating Cheese, Pepperoni, Bacon as additional decorations on a base pizza.
Is it for the reason that Cheese/Pepperoni have to be built seperately. I don't think, they need to be built seperately as they can be available readymade.
Pls clarify. Am also looking for a good real-world example of decorator pattern and reason why it is the apt for that particular example. Thank you.
From wikipedia's decorator pattern article:
In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing object dynamically.
There's no need to add toppings to a Pizza after it has been fully constructed. You don't eat half a pizza and then add another topping to it.
In other words, the Builder Pattern makes it easy to construct an object which is extensible in independent directions at construction time, while the Decorator Pattern lets you add extensions to functionality to an object after construction time. Using the decorator pattern to construct objects is bad because it leaves the object in an inconsistent (or at least incorrect) state until all the required decorators are in place - similar to the JavaBean problem of using setters to specify optional constructor arguments.