I'm reading this article:
http://www.codeproject.com/Articles/479635/UnderstandingplusandplusImplementingplusDecoratorp
I'm thinking of implementing this pattern in a school project. It's not a requirement so I can half-ass it. But, I just thought it would be a good opportunity to expand my knowledge and expertise.
The school project is this: Create a pizza ordering application where employees enter the orders of customers. So a pizza, and it can have any number of toppings.
The above article (and the description from the Head First: Design Patterns book) seem to match my application perfectly.
Here is my issue: This doesn't seem like a good pattern, and here is why:
Whenever "pizza place" adds a new topping to their menu.... they will have to add a whole new class, and recompile their ordering systems and re-distribute them?
I think perhaps the issue is that all of the examples I google have to deal with food and toppings of some sort.
The main disadvantage of decorator design pattern is code maintainability because this pattern creates lots of similar decorators which are sometimes hard to maintain and distinguish.
Advantage of Decorator PatternIt enhances the extensibility of the object, because changes are made by coding new classes. It simplifies the coding by allowing you to develop a series of functionality from targeted classes instead of coding all of the behavior into the object.
The decorator design pattern is one of the twenty-three well-known design patterns; these describe how to solve recurring design problems and design flexible and reusable object-oriented software—that is, objects which are easier to implement, change, test, and reuse.
The Decorator pattern provides a more flexible way to add responsibilities to a class than by using inheritance, since it can add these responsibilities to selected instances of the class. It also allows you to customize a class without creating subclasses high in the inheritance hierarchy.
Actually Decorator allows you to add some behavior without recompiling source code. You can declare IPizza
interface in your pizza domain, and use this abstraction in your application. Then you can add another assembly, which will have other implementations if IPizza
decorators, and inject those implementations to your application with dependency injection. Thus you will not need to recompile neither your application, nor domain.
BTW Adding new class is better, than modifying existing classes. You always can break something what was working before your modifications. That's why Single Responsibility Principle was introduced.
Another your questions:
Generally in real world applications you will be dealing with more abstract objects (ie not things liek pizzas and coffees :))
A real world example of Decorator pattern is the Java BufferedReader
class. It adds additional functionality to a FileReader
for example.
The benefits are that you can change behaviour at runtime and you are not tied down to having many many different objects.
In your example, if I have four objects:
Pizza
Tomatoes
Cheese
Mushrooms
Then I can build a pizza with any combination of the four ingredients. Otherwise I would have to have a ton of classes to allow that behaviour e.g. PizzaWithTomatoesAndCheese
, PizzaWithTomatoesAndMushrooms
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With