Factory methods

Object-Oriented Programming ใน Python ระดับกลาง

Jake Roach

Data Engineer

Conditional logic ในเมธอด

class Student:
    # Conditional logic with similar behavior for all conditions
    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)
Object-Oriented Programming ใน Python ระดับกลาง

Factory methods

Design pattern ที่ใช้ factory methods สร้างออบเจกต์เพื่อใช้ในเมธอดอื่น

  • คืนออบเจกต์ที่ implement interface
  • ลดความซับซ้อนในเมธอด
  • นำกลับมาใช้ใหม่ได้ เป็น modular
  • ใช้ _ นำหน้าเพื่อระบุ factory method

$$

มาดู products และ concrete products กันก่อน!

...

  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()
Object-Oriented Programming ใน Python ระดับกลาง

สร้าง Resource interface

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)

# Something similar for Blog, Video
Object-Oriented Programming ใน Python ระดับกลาง

ปรับปรุง explore_topic()

class Student:

    def explore_topic(self, resource_type, topic):
        if resource_type == "Textbook":
            texbook = Textbook()  # Creating an instance of class implementing Resource
            texbook.reference(topic)  # Call the reference() method

        elif resource_type == "Blog":
            blog = Blog()
            blog.reference(topic)

        elif resource_type == "Video":
            video = Video()
            video.reference(topic)
Object-Oriented Programming ใน Python ระดับกลาง

สร้าง factory method

class Student:
    # Factory method to return 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)  # Retrieve the resource
        return resource.reference(topic)
Object-Oriented Programming ใน Python ระดับกลาง

การใช้ factory methods

# Create a Student object, then have it reference a textbook
lester = Student()
lester.explore_topic("Textbook", "Object-oriented Programming")

# Each to switch to another resource
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"]
Object-Oriented Programming ใน Python ระดับกลาง

มาฝึกกันเถอะ!

Object-Oriented Programming ใน Python ระดับกลาง

Preparing Video For Download...