1. 학습
  2. /
  3. 강의
  4. /
  5. Python 중급 객체 지향 프로그래밍

Connected

연습 문제

팩터리 메서드 만들기

이제 팩터리 메서드를 직접 만들어 볼 준비가 되었네요. 팩터리 메서드 디자인 패턴을 사용해 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}""")

지침

100 XP
  • customer_type을 받는 _get_customer() 팩터리 메서드를 만들고, 해당 구체 제품의 객체를 반환하세요.
  • complete_transaction() 메서드에서, 앞서 정의한 팩터리 메서드를 사용해 고객 객체를 반환받은 뒤, make_payment() 메서드에 전달된 price로 결제를 진행하세요.