0
파이썬에서 추상 팩토리 패턴의 예제를 발견했습니다. DogFactory가 필요한 이유를 이해하려고 노력하고 있습니다. Dog 클래스를 호출하는 코드가 더 적지 않습니까?이 패턴이 실제 응용 프로그램에서 유용 할 수있는 방법에 대해 설명 할 수 있습니까?파이썬에서 추상 팩토리 패턴을 이해하려고 시도했습니다.
class Dog:
def speak(self):
return "Woof!"
def __str__(self):
return "Dog"
class DogFactory:
def get_pet(self):
return Dog()
def get_food(self):
return "Dog Food!"
class PetStore:
def __init__(self, pet_factory=None):
self._pet_factory = pet_factory
def show_pet(self):
pet = self._pet_factory.get_pet()
pet_food = self._pet_factory.get_food()
print("Our pet is '{}'!".format(pet))
print("Our pet says hello by '{}'".format(pet.speak()))
print("Its food is '{}'!".format(pet_food))
factory = DogFactory()
shop = PetStore(factory)
shop.show_pet()