vendredi 24 mars 2017

Calling non-decorated methods on decorator object

Suppose we have implemented the Decorator Pattern as in the image below. With the CondimentDecorator class, the Beverage class can be decorated. The methods cost() and getDescription() will take care for that.

enter image description here

When creating a decorated Beverage like below, calling the methods operation1() and operation2() won't work on the decorated object. In order to make this work, these two methods should also be placed in the class CondimentDecorator and delegate the call to the composed beverage variable.

Beverage b = new Milk(new Espresso());

When having a lot of methods which don't need decoration, all of these should also be placed in CondimentDecorator to simply delegate. So the question is: What is a neat (generally accepted) way to solve this 'problem'?

Aucun commentaire:

Enregistrer un commentaire