Return to Home Page
      Blog     Consulting     Seminars     Calendar     Books     CD-ROMS     Newsletter     About     FAQ      Search
 

"cafeMocha = Espresso(SteamedMilk(Chocolate(
Whipped(Decaf(Mug())))))"

One of the GoF's examples for this pattern was a way to put a border on a graphical component. Interestingly, Java's Swing team chose a different solution for that exact problem. This suggests an alternative pattern to the Decorator pattern, which I'll call the "Extras" pattern.

In this pattern, the object being decorated has a separate field for extras:


class Mug:
    def __init__(self, drink, extras):
        self.drink = drink
        self.extras = extras

    def getTotalCost(self):
        total = self.drink.getCost()
        for e in self.extras:
            total += e.getCost()
        return total

    def getDescription(self):
        d = self.drink.getDescription()
        for e in self.extras:
            d += ' ' + e.getDescription()
        return d

myDrink = Mug(Espresso(), [SteamedMilk(), Chocolate(),
                           Whipped(), Decaf()])

One advantage here is improved mutability. If the customer actually wanted Raspberry flavoring instead of Chocolate, you just do:

myDrink.extras[1] = Raspberry()

As a pleasant side effect, Mug now acts as a container class. :-)

A general design principle for using Python is: prefer the flat to the recursive. A corollary is: prefer Python lists to linked lists.

f  e  x



Add your comment below. Use an empty line between each paragraph. Paragraphs will be automatically formatted, and single carriage returns will be respected. Use <code> to begin a code block, and </code> to end a code block. Your email address will not be visible to spam harvesters or used in any way except to contact you with further questions.

Your Email Address:

Search     Home     WebLog     Consulting     Seminars     Calendar     Books     CD-ROMS     Newsletter     About     Contact     Site Design     Server Maintenance     Powered by Zope
©2007 MindView, Inc.