File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes decorator pattern from HFDP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "decorator pattern from HFDP" Watch "decorator pattern from HFDP" New topic
Author

decorator pattern from HFDP

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Has any body read decorator pattern from HFDP. I am confuse with their very first example.

Beverage class has a field description, a method getDescription() and an abstract method cost()...

All sub classes will implement cost() as per they want. But I am confuse with description field. How sub classes will use this description and getDescription()...

Please help.
Thanks.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

I havent understood your question .

When the subclass extends Beverage the subclasses will have the method getDescription in them.


Groovy
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Pradip Bhat:
I havent understood your question .

When the subclass extends Beverage the subclasses will have the method getDescription in them.


Yes.

But when we will call this getDescription() method from super class reference that is referring to sub class object (I am sure, we will do it like this only) then it will print description of super class (not the description of individual sub classes).

Like this:



I hope, now my point is clear.
Please comments.
Thanks a lot.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
A common idiom is to provide the value for a field in the constructor, so that different subclasses can initialize it differently. Not sure wether the HF example does this, though.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


oops... I missed it. We can always override method in sub class if we want some different description.




A common idiom is to provide the value for a field in the constructor, so that different subclasses can initialize it differently. Not sure wether the HF example does this, though.


Yes, got it.

Thanks Pradip and Ilja
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
...But they have not overridden the getDescription() method in sub classes.

Thanks.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
How is the description initialized in the base class?
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Ilja Preuss:
How is the description initialized in the base class?


Actually, they have not given the code. But I think, initilizing at the time of declaration is the only option...

Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
As I tried to say above, there *is* another option, one that gives subclasses the ability to specificy an individual description:

Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I thought this was aobut the decorator pattern though?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Mr. C Lamont Gilbert:
I thought this was aobut the decorator pattern though?


Yes. Super, in this context, is the abstract base class for all Decorator implementations, the way I understand it.
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385

...But they have not overridden the getDescription() method in sub classes.


They did. Page 95 super of all beverages Beverage returns default value "Unknown Beverage". Page 97 derrived Mocha overrides getDescription() :

Thomas Andersson
Greenhorn

Joined: Sep 14, 2004
Posts: 3
They have set the description attribute in the subclass constructor (only done for the "base" ingridient).



This could of course also be done by making the description attribute private and adding a constructor that takes a string, as mentioned above. The solution with the added constructor is perhaps cleaner.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Thomas Andersson:
They have set the description attribute in the subclass constructor (only done for the "base" ingridient).



This could of course also be done by making the description attribute private and adding a constructor that takes a string, as mentioned above. The solution with the added constructor is perhaps cleaner.


Definitely!
Shafique Razzaque
Greenhorn

Joined: Mar 03, 2004
Posts: 11
My input on this topic:

The reason description method has an implmentation in the base class (Beverage) is becoz: all the Concrete Components (Expresso, HouseBlend etc) need not override it. Description is a simple method all ConcreteComponents may simply inherit and reuse without any change.

However the example shows us to decorate the description and cost(in ConcreteDecorators). So, to enforce the ConcreteDecorators to implement their own getDescription method..it is made abstract in the Decorator interface (CondimentDecorator).

As for the getCost method, it needs to be overriden at all ConcreteComponents and decorated at the ConcreteDecorators. Hence it is made abstract at the base class itself (at Beverage).

Hope it clarifies your doubt.

Cheers,
Shafique Razzaque,
Singapore.


Shafique Razzaque,<br />Senior Software Engineer,<br />Singapore.<br /> <br />Masters (in progress), SCEA (in progress), SCBCD (98%), SCJP (76%)
Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
hi

# Shafique i agree with u

BUT i think this is unnecessary done to complicated.
imo, there is no need for implementation of getDescription() in Beverage
and then make it abstract in CondimentDecorator.
For what is the description with the value of �Unknown Beverage� for?
Insteed why not just make getDescription() abstract inside Beverage
and omit it from CondimentDecorator?
That way u will still enforce the ConcreteDecorators to implement getDescription() BUT it is MUCH MORE SIMPLE.

Any drawbacks to this?
Shafique Razzaque
Greenhorn

Joined: Mar 03, 2004
Posts: 11
In reply to Ner min's comment:

The only drawback would be:
Every Concrete Component will also have to write the getDescription() method.
Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
???
but i thought the whole Idea is to force every ConcreteComponent to write the getDescription() implementation.
That's what for r Decorators for: they add extensons by reimplementing.
Again:
For what is the description with the value of �Unknown Beverage� for?

Imagine that someone ask u:
1:Hi Mr. would u like a niche fresh Unknown Beverage?
2 h hm...
1:with suger?
[ October 27, 2005: Message edited by: Ner min ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: decorator pattern from HFDP