Question about decorator pattern and the abstract decorator class?
This question already has an answer here:
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 Beverage
with milk and nutmeg. Using decorators based on the abstract decorator class, you merely wrap with with Milk
and Nutmeg
decorators. If it was derived from Beverage
, you'd have to have a MilkWithNutmegBeverage
class and a MilkBeverage
class and a NutmegBeverage
class. 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.
Better one and a half year late than never:
A base class for decorators of a certain interface is not necessary, but it is very useful to have.
It is useful, for one thing as a means of documenting that classes derived from it are decorators of the interface in question, but mostly because decorators usually do not need to add functionality to every single method of the decorated interface, so a base decorator class allows derived decorators to implement only those methods of the interface to which they actually need to add some functionality, leaving the rest of the methods to the base class to provide a default implementation for. (Which simply delegates the call to the decoree.)
Contrast this with writing decorators that implement the decorated interface from scratch, where the compiler requires that you provide an implementation for every single method of the interface, whether your decorator will be adding any functionality to it, or not.
It is that simple, really.
I was wondering the same thing. Going back to the source, GOF Design Patterns, I see this under 'Implementation' in the Decorator chapter:
"Omitting the abstract Decorator class. There's no need to define an abstract Decorator class when you only need to add one responsibility. That's often the case when you're dealing with an existing class hierarchy rather than designing a new one. In that case, you can merge Decorator's responsilibility for forwarding requests to the component into the Concrete Decorator."
So at least in that case, it seems that GOF agree with you :-)
I'm not sure what the meaning of 'one responsibility' is. I'm not sure if more than 'one responsibility' would mean one concrete decorator that has more than one responsibility or more than one concrete decorator, each with its one responsibility. Either way, I don't see why the abstract Decorator is necessary. My guess is that tvanfosson's answer (in his comment on his own answer) is the right one - that once you start creating a number of decorating classes, it clarifies the design decision to group them under a superclass. On the other hand, where there is just one class, it perhaps makes the design decision less clear if you add in a second class that just sits as a pointless middle-man between base component and decorator (having said that, it's fairly likely that you'll want to add more at some point, so maybe better to include the abstract decorator even in the single case...)
At any rate, seems like it's to do with making the design clear, rather than the being the difference between the code working and not.
链接地址: http://www.djcxy.com/p/32002.html上一篇: 使用装饰模式的观察者模式
下一篇: 关于装饰模式和抽象装饰类的问题?