While reading on Decorator pattern, came across something that needed to be clarified. In decorator pattern we just wrap a given object and used a chain of executions for calculation. But why can’t we have a list of those objects and iterate through each. I mean without having the chain, can’t we just use a list and simple iteration through each object ?
Thanks
I believe you have missed the point of Decorator.
Decorator aims to add behavior transparently. The classic example is InputStream in Java. You can chain like buffering, gzip feature to an input stream. However, the “user” of that decorated input stream do not need to know there is extra behavior added. The user simply use that decorated stream as a normal stream. Of course it will work if you have each “behavior” as a separate object, and store the chain as another list and invoke them explicitly and separately. However it lost the “transparency” in decorator.