• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

what if decorator is not component?

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
�Decorator� should also have a same type as �component� (object going to be wrapped or decorated). What if �decorator� is NOT a �component� in real world?

For example, in HFDP, example made each condiment is a beverage. Which is not true in real sense?

Thanks.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The real world "is a" relationship is only a very rough heuristic for source code inheritance relationships.

On the other hand, if you can't find a good name that seems to reflect the code relationship, it might be valuable to rethink the design.

I'm not fully familiar with the HFDP example, but I have the feeling that this is mostly a matter of non-ideal naming. For example, if we have a decorator that adds ice to a beverage, a good name might be IcedBeverage (similar to a BufferedInputStream being a decorator adding a buffer to an InputStream).

In general, though, the forces in the code trump real world relationships, in my opinion.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search this forum as this topic has been discussed before. I still am not convinced that HFDP's example of the decorator pattern was the best choice (I searched 'beverage', but 'condiment' might have more hits). You will probably get various opinions on the matter.

It is probably most important that you understand what the benefits of the decorator pattern are and not focus on this particular example.

Here is one of the links that discussed the issue: https://coderanch.com/t/99387/patterns/HeadFirst-Design-Patterns-Decorator-Chapter

[ June 04, 2007: Message edited by: steve souza ]
[ June 04, 2007: Message edited by: steve souza ]
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice link Steve. Thanks!
Thanks Ilja. If I understood correctly you are proposing to have a different name for condiment that seems like beverage, to map it with real model.

I doubt it is always clear.

Say, I have a Room interface and ConferenceRoom component class implementing Room. Some client code is expecting Room somewhere...
Now I want to decorate ConferenceRoom with Painting (a decorator class).

What can be the name of Painting so it seems like Room? A PaintedRoom? Yes, it seems like Room but doesn't seem like Painting, which it is actually.

My point is, somewhere we are compromising in mapping it with real model.

Also, say if some other client code is using Painting class (away from decorator), for example: Rick draws paintings

class Rick {
Painting[] ownPaintings = null;
}

Now after applying decorator pattern, how it will look:

class Rick {
PaintedRoom[] ownPaintings = null;
}

This again confuses other developers, what�s going on here???



Thanks.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Real - world style aside, Painting class would not be a good fit to decorate Room class. A Painting is not a special type of Room with additional functionality from a raw Room. Just because a Painting can decorate the walls of a Room, doesn't mean that its a good fit as a Room decorator from an OO pattern perspective.
[ June 05, 2007: Message edited by: Garrett Rowe ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On the other hand, maybe a RoomWithPaintings:

 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or a DrapedWallsRoom.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, choosing a better name sounds good.

Thanks!
reply
    Bookmark Topic Watch Topic
  • New Topic