Python 중급 객체 지향 프로그래밍
Jake Roach
Data Engineer
class Student:
# 모든 조건에서 유사한 동작의 조건문
def explore_topic(self, resource_type, topic):
if resource_type == "Textbook":
print(f"Referencing {topic} using a textbook")
self.reference_textbook(topic)
elif resource_type == "Blog":
print(f"Make sure to validate information provided in blogs!")
self.reference_blog(topic)
elif resource_type == "Video":
self.reference_video(topic)
다른 메서드에서 사용할 객체를 생성하기 위해 팩토리 메서드를 쓰는 디자인 패턴
_로 표시$$
먼저 제품과 구체 제품을 살펴봅시다.
...
def _get_resource(self, resource_type):
if resource_type == "Textbook":
# Textbook, Blog and Video are
# all resources
return Textbook()
elif resource_type == "Blog":
return Blog()
elif resource_type == "Video":
return Video()
class Resource(ABC):
@abstractmethod
def reference(self, topic):
pass
class Textbook(Resource):
def __init__(self):
self.index = {"Object-oriented Programming": ["Inheritance", ...]}
def reference(self, topic):
print(f"Referencing {topic} using a textbook")
return self.index.get(topic)
# Blog, Video도 유사
class Student:
def explore_topic(self, resource_type, topic):
if resource_type == "Textbook":
texbook = Textbook() # Resource를 구현한 클래스의 인스턴스 생성
texbook.reference(topic) # reference() 호출
elif resource_type == "Blog":
blog = Blog()
blog.reference(topic)
elif resource_type == "Video":
video = Video()
video.reference(topic)
class Student:
# Resource를 반환하는 팩토리 메서드
def _get_resource(self, resource_type):
if resource_type == "Textbook":
return Textbook()
elif resource_type == "Blog":
return Blog()
elif resource_type == "Video":
return Video()
def explore_topic(self, resource_type, topic):
resource = self._get_resource(resource_type) # 리소스 가져오기
return resource.reference(topic)
# Student 객체를 만들고, 교재를 참조해 보세요
lester = Student()
lester.explore_topic("Textbook", "Object-oriented Programming")
# 다른 리소스로 전환
lester.explore_topic("Video", "Object-oriented Programming")
Referencing Object-oriented Programming using a textbook
["Inheritance", "Constructors", "Class-level methods"]
Video are helpful for visual or audatory learners
["Classes", "Methods", "Attributes", "self"]
Python 중급 객체 지향 프로그래밍