简体   繁体   中英

Factory Method Design Pattern in Python

I'm studying Factory Method Design Pattern in Python. I understand the point of using it in principle but still some aspects got me confused when I experiment with the code.

I have made two examples, pizza_factory_1.py using interfaces and pizza_factory_2.py without interfaces:

pizza_factory_1.py :

class PizzaInterface:
    def prepare_pizza(self): pass  

class MozzarellaPizza (PizzaInterface):
    def prepare_pizza(self):
        print("Prepare Mozzarella pizza:", type(self))

class CaesarPizza (PizzaInterface):
    def prepare_pizza(self):
        print("Prepare Caesar pizza:", type(self))

class PizzaFactory:
    @staticmethod
    def get_pizza(pizza_type):
        pizza_type.prepare_pizza()

if __name__ == "__main__":
    mozzarella_pizza = PizzaFactory().get_pizza(MozzarellaPizza())
    caesar_pizza = PizzaFactory().get_pizza(CaesarPizza())

When executing this:

> Prepare Mozzarella pizza: <class '__main__.MozzarellaPizza'>
> Prepare Caesar pizza: <class '__main__.CaesarPizza'>

> Process finished with exit code 0

pizza_factory_2.py :

class MozzarellaPizza ():
    def prepare_pizza(self):
        print("Prepare Mozzarella pizza:", type(self))

class CaesarPizza ():
    def prepare_pizza(self):
        print("Prepare Caesar pizza:", type(self))

class PizzaFactory:
    @staticmethod
    def get_pizza(pizza_type):
        pizza_type.prepare_pizza()

if __name__ == "__main__":
    mozzarella_pizza = PizzaFactory().get_pizza(MozzarellaPizza())
    caesar_pizza = PizzaFactory().get_pizza(CaesarPizza())

When executing this:

> Prepare Mozzarella pizza: <class '__main__.MozzarellaPizza'>
> Prepare Caesar pizza: <class '__main__.CaesarPizza'>

> Process finished with exit code 0

So, the output is exactly the same. The Design Pattern description says the following:

This results in increased flexibility and reuse of code because the shared functionality will not be rewritten having been inherited from the same class.

So, what's the advantage of using interfaces in this case? In pizza_factory_2.py example there's also no need to rewrite shared functionality. I can call PizzaFactory.get_pizza function and feed in any type of object.

I can't say anything about the Factory Method Design Pattern because neither your code, nor the link you provide, nor your actual question is about that.

Your actual question: "So, what's the advantage of using interfaces in this case?" is easier to answer.

I think you have decided for yourself that the code in pizza_factory_2.py is better and I think your reason is that you have avoided creating class PizzaInterface . In my opinion this is entirely correct because you have reduced the number of classes involved. See this article by Martin Fowler where fewest elements is mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM