James Relic
James Relic

Reputation: 85

Decorator Design Pattern tied to same interface

I have one slight issue with the decorator design pattern. It seems that the decorated objects are tied to the same interface as a standard non-decorated object.

Please see the example of the website line below. https://www.tutorialspoint.com/design_pattern/decorator_pattern.htm

In this example the RedShapeDecorator is tied to the shape interface so both the RedShapeDecorator and a standard Circle object can only call the draw() method.

Well what do I do when I want my decorated object to be able to call more then draw(). What if I want my decorated object to have methods like drawBlackAndWhite() and draw3D() both of which are not appropriate to wrap up in the draw() method.

In other words I want to be able to extend the RedShapeDecorator to do the following..

redShapeDecorator.draw()
redShapeDecorator.drawBlackAndWhite();
redShapeDecorator.draw3D();

But the Shape interaface is limiting me to only calling redShapeDecorator.draw(). How do I solve this?

Upvotes: 0

Views: 227

Answers (1)

Oleksandr
Oleksandr

Reputation: 818

I'm not sure if I get the question right, but if you want a method drawBlackAndWhite()(assuming it's analogue of RedShapeDecorator.draw()) you can define additional method in your decorator. To get draw3D() method in your decorator you should create a new implementation of Shape interface which capable to draw 3D objects(because existing implementations Circle and Rectangle are flat). If you want all these methods in one instance of your decorator put multiple Shape fields in it. But you won't be able to call methods drawBlackAndWhite() and draw3D() by reference with type Shape because Shape interface has only one method draw()

Upvotes: 1

Related Questions