工厂模式的本质就是用工厂方法代替new操作创建一种实例化对象的方式.

python设计模式之工厂模式(Factory Pattern)_父类


这里专门定义一个Operation类作为父类,加减乘除运算类继承Operation类,OperationFactory类用来决定什么时候创建对应的类

import random
from typing import Type


class Pet:
def __init__(self, name: str) -> None:
self.name = name

def speak(self) -> None:
raise NotImplementedError

def __str__(self) -> str:
raise NotImplementedError


class Dog(Pet):
def speak(self) -> None:
print("woof")

def __str__(self) -> str:
return f"Dog<{self.name}>"


class Cat(Pet):
def speak(self) -> None:
print("meow")

def __str__(self) -> str:
return f"Cat<{self.name}>"


class PetShop:

"""A pet shop"""

def __init__(self, animal_factory: Type[Pet]) -> None:
"""pet_factory is our abstract factory. We can set it at will."""

self.pet_factory = animal_factory

def buy_pet(self, name: str) -> Pet:
"""Creates and shows a pet using the abstract factory"""

pet = self.pet_factory(name)
print(f"Here is your lovely {pet}")
return pet


# Additional factories:

# Create a random animal
def random_animal(name: str) -> Pet:
"""Let's be dynamic!"""
return random.choice([Dog, Cat])(name)


if __name__ == '__main__':
cat_shop = PetShop(Cat)
pet = cat_shop.buy_pet("Lucy")
pet.speak()

shop = PetShop(random_animal)
for name in ["Max", "Jack", "Buddy"]:
pet = shop.buy_pet(name)
pet.speak()
print("=" * 20)