File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes Doubt about Design Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "Doubt about Design Pattern" Watch "Doubt about Design Pattern" New topic

Doubt about Design Pattern

Sreeraj G Harilal
Ranch Hand

Joined: Apr 19, 2006
Posts: 310
I have a doubt about decorator design pattern.

Codes with decorator

Codes without decorator

This is the main class

With both set of classes, when we execute main class the result will be the same. If that is the result, what the decorator is?

SCJP 5.0<br />SCWCD 1.4<br />Preparing for <b>SCEA</b>.<br /><b>"I prefer an interesting vice to a virtue that bores."</b>
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11778

"Sreeraj G H",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You might also think about making a shorter question, otherwise known as isolating the problem. I know that when I see a post as long as yours, I tend to ignore it until I have enough time to read it. There are many other ranchers who feel the same way.

JavaRanch Sheriff

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Frank Kuepper
Ranch Hand

Joined: Mar 16, 2007
Posts: 45
Hi Sreeraj,

you may not have noticed, but the second version of code, you provided, uses the decorator pattern as well

The original design makes a distinction between the basic beverage (the coffee variations) and the add-ons (milk, whip and so on), whereas you combined all of them to extend a common base class.

Although your approach is valid as well, I yould prefer the original one, as it is clearer in terms of valid variations: with your approach you could shake a "Mocha with Decaf" - kind of odd, isn't it


SCEA (93%/93%)
I agree. Here's the link:
subject: Doubt about Design Pattern
It's not a secret anymore!