• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing Decorator From The Decorated Object

 
Thennam Pandian
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading HeadFirst Design pattern book. In the book they have explained the decorator pattern with "coffee" menu.

Consider the following scenario :

1. Customer has added Expresso with mocha and soy.

2. Now the Expresso is decorated with mocha and soy.

3. Suddenly the customer want to replace soy with Whip.

In this case, we need to remove the soy from the decorated object and decorate with Whip.

Is it possible to remove decorator from the decorated Object? if yes, please explain.

or

decorator talks about only adding dynamic responsibility to the component ?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could "remove" the decorator by providing a getter for the decorated object and continuing with that reference and discarding the decorator reference.
 
Thennam Pandian
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

I have tried the following and it is working fine.

I have provided one method called "getPrevBeverage" in CondimentDecorator.

This method will return the decorated object before adding the current decorator object.

With this implementation, am able to solve the above problem.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding a getter to retrieve a previous decorator doesn't actually make a whole lot of sense to me.
Consider that the intent of a decorator is to add behaviour dynamically to an instance of some type (class or interface), and that instance can be decorated multiple times. How are you ever supposed to know which decorator reference - or indeed a reference to the original undecorated instance - will be returned. It's completely dependent on the order in which the decorators were applied. Further more, if you are strictly programming to an interface, all you'll have available to you is the interface of the orginal undecorated type. Which would need to specify a getter that returns an instance of itself, soley for the purpose of obtaining a previous decorator reference - something it shouldn't have any knowledge about in the first place. This seems odd to me. Come to think of it, I don't think I've ever encountered a decorator implementation that allowed for this kind of unwrapping behaviour, and I'm struggling to come up with a use case for it.
 
Paul Clapham
Sheriff
Pie
Posts: 20980
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:I don't think I've ever encountered a decorator implementation that allowed for this kind of unwrapping behaviour, and I'm struggling to come up with a use case for it.


My approach would be to just keep a reference to the original object. Don't make the decorator return a reference to the decoratee (the thing being decorated), just keep your own reference to it.

It's also worth mentioning that in real life it's often a bad idea to work with the undecorated object, but in this case this is just one of those silly contrived examples.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Jelle Klap wrote:I don't think I've ever encountered a decorator implementation that allowed for this kind of unwrapping behaviour, and I'm struggling to come up with a use case for it.


My approach would be to just keep a reference to the original object. Don't make the decorator return a reference to the decoratee (the thing being decorated), just keep your own reference to it.

It's also worth mentioning that in real life it's often a bad idea to work with the undecorated object, but in this case this is just one of those silly contrived examples.


Exactly, just retain a reference to the original.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic