1

This is how I gathered a decorator pattern would work in Python after looking at this answer. Can you help me understand if it's not what the Decorator pattern is all about?

Here, a pizza is ordered with toppings. Adding toppings (the decoration)is done using functions used in chain. The price is updated as the decoration is done and finally a Pizza object with the final price is returned, which is then ordered.

def api_send_order(pizza):
    print('Ordered pizza for '+str(pizza.price)+' with toppings of '+str(pizza.toppings))
    return True

# designed:

class Pizza():
    def __init__(self):
        self.price = 100
        self.toppings = list()
    def order(self):
        return api_send_order(self)

def mushroom_topped(pizza):
    pizza.price += 20
    pizza.toppings.append('mushroom')
    return pizza

def extracheese_topped(pizza):
    pizza.price += 10
    pizza.toppings.append('extra-cheese')
    return pizza

# used as:

mushroom_topped(extracheese_topped(Pizza())).order()
Community
  • 1
  • 1
Nir Lanka
  • 379
  • 1
  • 4
  • 13
  • 1
    Check this link: http://python-3-patterns-idioms-test.readthedocs.io/en/latest/PythonDecorators.html – Roshan Nov 20 '16 at 11:08
  • 1
    its not pretty much clear what classes you got and the interactions. I can only see class 'Pizza'. Can u pls explain in question or improve your code? :)) – Supun Wijerathne Nov 28 '16 at 04:06

0 Answers0