This question was asked already here, but rather than answering the specific question, descriptions of how the decorator pattern works were given instead. I’d like to ask it again because the answer is not immediately evident to me just by reading how the decorator pattern works (I’ve read the wikipedia article and the section in the book Head First Design Patterns).
Basically, I want to know why an abstract decorator class must be created which implements (or extends) some interface (or abstract class). Why can’t all the new “decorated classes” simply implement (or extend) the base abstract object themselves (instead of extending the abstract decorator class)?
To make this more concrete I’ll use the example from the design patterns book dealing with coffee beverages:
- There is an abstract component class called
Beverage - Simple beverage types such as
HouseBlendsimply extendBeverage - To decorate beverage, an abstract
CondimentDecoratorclass is created which extendsBeverageand has an instance ofBeverage - Say we want to add a “milk” condiment, a class
Milkis created which extendsCondimentDecorator
I’d like to understand why we needed the CondimentDecorator class and why the class Milk couldn’t have simply extended the Beverage class itself and been passed an instance of Beverage in its constructor.
Hopefully this is clear…if not I’d simply like to know why is the abstract decorator class necessary for this pattern? Thanks.
Edit: I tried to implement this, omitting the abstract decorator class, and it seems to still work. Is this abstract class present in all descriptions of this pattern simply because it provides a standard interface for all of the new decorated classes?
It enables the decoration of the base class independently with various decorators in different combinations without having to derive a class for each possible combination. For example, say you want your
Beveragewith milk and nutmeg. Using decorators based on the abstract decorator class, you merely wrap with withMilkandNutmegdecorators. If it was derived fromBeverage, you’d have to have aMilkWithNutmegBeverageclass and aMilkBeverageclass and aNutmegBeverageclass. You can imagine how this explodes as the number of possible combinations increases. Using the abstract decorator implementation reduces this to just one decorator class implementation per decoration.