I’ve attended a session about decorator pattern where most of the examples provided were in java.For instance in the example below the Pizza object is being decorated with Two toppings .
Pizza vegPizza = new ToppingsType1(new ToppingType2(new Pizza()))
In python i’ve seen decorators being used in the scenarios like this
@applyTopping2
@applyTopping1
makePizza():
pass
Though here I can see that the the makePizza function is being decorated with two functions , but it differs a lot from the class based approach of java.
My question is do python decorators strictly implement the decorator pattern or do the implementation is a little different but the idea is same.
PS: I am not sure where to lookup the standard definition of the decorator pattern.though wikipedia gives an example in a dynamic language (JS) but my question still holds good 🙂
You should read this which explains what are python’s decorators.
So, you will be able to implement the “classical” decorator pattern with python’s decorator, but you will be able to do much more (funny ;)) things with those decorators.
In your case, it could looks like :
And then you will get a Margherita 🙂