Using the coffee decorator example shown on Wikipedia ( http://en.wikipedia.org/wiki/Decorator_pattern) how would it be possible for someone to be able to have methods that only the decorators have, for example, the milk decorator could have a method called “fatContent”. Is this even possible with this type of design pattern? If not, what kind of pattern could I use to accomplish this?
Using the coffee decorator example shown on Wikipedia ( http://en.wikipedia.org/wiki/Decorator_pattern ) how would it
Share
Decorator pattern by definition does not allow adding methods other than those that are defined in the interface. Actually you can always add methods to any class but once these methods are not defined in the implementing interface client cannot call them using this interface.
Simple solution to your problem is to define several interfaces, e.g. Coffee and Milk. Then you can define class
Capuchinothat implements both interfaces and probably holds 2 instances:SimpleCofeeandFoamedMilk. But this solution looks more as a combination of Decorator and Facade.