www.carfield.com.hk
CoffeeShop.py
2001-12-26T16:00:00Z
2001-12-26T16:00:00Z
<br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: cX:decorator:compromise:CoffeeShop.py
# Coffee example with a compromise of basic
# combinations and decorators
class DrinkComponent:
def getDescription(self):
return self.__class__.__name__
def getTotalCost(self):
return self.__class__.cost
class Espresso(DrinkComponent):
cost = 0.75
class EspressoConPanna(DrinkComponent):
cost = 1.0
class Cappuccino(DrinkComponent):
cost = 1.0
class CafeLatte(DrinkComponent):
cost = 1.0
class CafeMocha(DrinkComponent):
cost = 1.25
class Decorator(DrinkComponent):
def __init__(self, drinkComponent):
self.component = drinkComponent
def getTotalCost(self):
return self.component.getTotalCost() + \
DrinkComponent.getTotalCost(self)
def getDescription(self):
return self.component.getDescription() + \
' ' + DrinkComponent.getDescription(self)
class ExtraEspresso(Decorator):
cost = 0.75
def __init__(self, drinkComponent):
Decorator.__init__(self, drinkComponent)
class Whipped(Decorator):
cost = 0.50
def __init__(self, drinkComponent):
Decorator.__init__(self, drinkComponent)
class Decaf(Decorator):
cost = 0.0
def __init__(self, drinkComponent):
Decorator.__init__(self, drinkComponent)
class Dry(Decorator):
cost = 0.0
def __init__(self, drinkComponent):
Decorator.__init__(self, drinkComponent)
class Wet(Decorator):
cost = 0.0
def __init__(self, drinkComponent):
Decorator.__init__(self, drinkComponent)
cappuccino = Cappuccino()
print cappuccino.getDescription() + ": $" + \
`cappuccino.getTotalCost()`
cafeMocha = Whipped(Decaf(CafeMocha()))
print cafeMocha.getDescription() + ": $" + \
`cafeMocha.getTotalCost()`
#:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
2001-12-26T16:00:00Z