dimanche 20 novembre 2016

Is this an example of Decorator Pattern?

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?

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

# pattern implemented:

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

def mushroom_topped(pizza):
    pizza.price += 20
    return pizza

def extracheese_topped(pizza):
    pizza.price += 10
    return pizza

# pattern used as:

mushroom_topped(extracheese_topped(Pizza())).order()

Aucun commentaire:

Enregistrer un commentaire