Using Lists Instead of Decorator Pattern?

Decorator pattern is about decorating ( enhancing ) an object with additional features at runtime.

Suppose you already have a class, lets call it class A which implements an interface IA. Now if there is a requirement of adding an additional feature for which we want it to have a method ,someAlignFeatureToA() which was not there in A. Now you have an option of extending the class A. Another approach is Composition which should be favoured over Inheritance. You can wrap the object of class A in another class B implwmenting the same Interface as of A i.e. IA. This way for the client code it would be easy to accept the object of class B as it has the same interface as of A. (Assumption is the code is well written which depends on the Abstractions (interface IA) and not on concrete classes like class A).

This way the inheritance chain is not too long and you can add additional features at runtime easily.

Now coming to your question : Yes in the example you took in your question, a List fits better, but I feel its the intention of the author to explain usuage of Decorator pattern with an easy example. Suppose Coffee is made up of milk, coffee mix and sugar. Coffee mix is further made up of smaller components . Here the solution emerges similar to Composite pattern.

Decorator pattern is design pattern based on Enhancement of Behavior. Java IO streams uses this where behavior (method implementation) is enhanced by the decorating class (one stream is wrapped with another to enhance the previous one)


What you are telling is a way to calculate the total cost of the beverage. In more general terms you are proposing an alternative way to merge & execute the main behavior of each of the derived objects supposed by the Decorator Pattern, by adding up them into a list. And it is not quite an OO approach too.

But the original intent of Decorator Pattern goes to a much wider perspective other than that. It is Adding extended functionalities to an object dynamically. Which means I have some object O1 & I want it to convert it into another object O2 with extended functionality & yet to be those 2 are interchangeable.

So it is about how we can manage the Object Evolution against the Object Lifecycle. Hope you got the idea. :))