팩터리 메서드 만들기
이제 팩터리 메서드를 직접 만들어 볼 준비가 되었네요. 팩터리 메서드 디자인 패턴을 사용해 Checkout 클래스를 만드는 것부터 시작하겠습니다. 아래에는 여러분을 위해 정의된 Customer 인터페이스와 RewardsMember, NewCustomer 구체 제품이 제공되어 있습니다. 아래 코드를 확인해 보세요!
class Customer(ABC):
@abstractmethod
def make_payment(self, price):
pass
class RewardsMember(Customer):
def make_payment(self, price):
print(f"""Total price for rewards member is
${price * .90}, which is 10% off""")
class NewCustomer(Customer):
def make_payment(self, price):
print(f"""Total price for new customer is ${price}""")
이 연습은 강의의 일부입니다
Python 중급 객체 지향 프로그래밍
연습 안내
customer_type을 받는_get_customer()팩터리 메서드를 만들고, 해당 구체 제품의 객체를 반환하세요.complete_transaction()메서드에서, 앞서 정의한 팩터리 메서드를 사용해 고객 객체를 반환받은 뒤,make_payment()메서드에 전달된price로 결제를 진행하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
class Checkout:
# Create a _get_customer() factory method
def ____(self, customer_type):
if ____ == "Rewards Member":
return ____()
elif ____ ____ "New Customer":
return ____()
# Define the complete_transaction() method
def complete_transaction(self, customer_type, price):
customer = ____.____(____)
____.____(____)