• 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

The decorator pattern. HFDP

 
Ranch Hand
Posts: 1162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
page 83 of HFDP has me lost. I'm a bit confused. why do we need to have all those boolean variables for milk, soy, mocha, whip? aren't we assuming that all classes that implement this abstract class Beverage will have those condiments ie; they will all be true? Also where are variables like milkCost,soyCost,mochaCost coming from?
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chunnard Singh:
they will all be true?



It's unlikely that all of them will be true. Someone may want a HouseBlend "Black" (i.e. no condiments whatsoever) in which case all of them are false. it is also unlikely that someone will want soy and milk in the same beverage.

If anything you could complain that those properties shouldn't be boolean - they should be a quantity. A milk shot into a standard coffee isn't going to cost as much as the milk in a latte, which consist 2/3 of milk.

This particular domain model assumes that the cost for each condiment is constant across all beverages - but only the condiments that are used in the preparation of a particular beverage are factored into the cost.

Also where are variables like milkCost,soyCost,mochaCost coming from?



I presume those are private attributes of the Beverage class that represent the cost of each condiment (if it is used in the beverage preparation) - they could even be private constants. To reduce clutter you typically only show public methods and attributes in UML. If you need to show more then you use prefixes: "+" for public, "#" for protected, and "-" for private.

This way only the Beverage class needs to know the cost of all the possible condiments. Meanwhile the subclasses only need to know their own basic cost and which condiments need to be added. - its the Beverage class that calculates the combined cost of those condiments.

Also remember that this has got nothing to do with the decorator pattern. It merely sets the stage to replace this design with decorators.
reply
    Bookmark Topic Watch Topic
  • New Topic