I am learning design patterns and trying to follow Go4 book. On page:179, in the decorator pattern chapter, there is a line which says
“..by extending the number of strategies from just one to an
open-ended list, we achieve the same effect as nesting decorators
recursively.”
I didn’t quite get this statement.
Strategies focus on having independent algorithms, which can be set dynamically and don’t know much about the client they are set in.
Whereas decorators are not quite independent of the clients they decorate. In fact, they are of same supertype as the object they decorate.
Am I missing a point here?
I’ll quote a little more of the context that I think is needed for this to make sense.
All this is saying is that both patterns can be used to add behavior to your base component, and that with Decorator, to add multiple behaviors, you can nest the decorators, while with Strategy, you need to use multiple strategies.
You’re right that strategies are generally more independent of the main component than decorators, but it is possible for them to be aware of the component. And to use the Strategy pattern, the main component is aware of the existence of strategies, where that’s not needed with Decorator.