Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt about Design Pattern

 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.

Andrew
JavaRanch Sheriff
 
Frank Kuepper
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Greetings,
Frank
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic