Interfaces

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

Jake Roach

Data Engineer

Interfaces

คลาสชนิดพิเศษที่ประกอบด้วย abstract method เท่านั้น ซึ่งสร้าง สัญญา กับคลาสที่ implement interface นั้น

  • ต้องกำหนด abstract method
  • โครงสร้างของ method definitions
  • พารามิเตอร์ไม่จำเป็นต้องตรงกัน
  • แบบ formal หรือ informal

$$

$$

เริ่มด้วย informal interfaces กันก่อน!

แผนภาพ Python interface ที่มี abstract method สามตัว

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

Informal interfaces กับ duck typing

class Course:
    def assign_homework(self, assignment_number, due_date):
        # Typically, the pass keyword will be used when creating an interface
        pass  

    def grade_assignment(self, assignment_number):
        pass
  • Duck typing
  • assign_homework() และ grade_assignment() เป็น abstract method
Object-Oriented Programming ใน Python ระดับกลาง

Informal interfaces กับ duck typing

class ProgrammingCourse:
    def __init__(self, course_name):
        self.course_name = course_name

    def assign_homework(self, due_date):
        # Some implementation of assign_homework here...

    def grade_assignment(self, assignment_number):
        # Some implementation of grade_assignment here...
Object-Oriented Programming ใน Python ระดับกลาง

Formal interfaces กับ ABC, @abstractmethod

from abc import ABC, abstractmethod

class Course(ABC):    
    @abstractmethod
    def assign_homework(self, assignment_number, due_date):
        pass

    @abstractmethod
    def grade_assignment(self, assignment_number):
        pass

formal interfaces แตกต่างจาก abstract base class อย่างไร?

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

Interfaces vs. Abstract Base Classes

Interfaces

  • กำหนด "สัญญา" กับคลาสที่ implement
  • "Must-do"
  • แบบ formal และ informal
  • มีเฉพาะ abstract method (ไม่มี concrete method)
  • คลาสที่แทบจะเหมือนกัน

Abstract base classes

  • สร้าง "blueprint" สำหรับคลาสที่มีลักษณะร่วมกัน
  • "Is-a"
  • ส่วนใหญ่เป็นแบบ formal
  • มีทั้ง abstract และ concrete method
  • คลาสที่มีลักษณะและพฤติกรรมคล้ายกัน
Object-Oriented Programming ใน Python ระดับกลาง

การ implement formal interface

class ProgrammingCourse(Course):
    def __init__(self, course_name):
        self.course_name = course_name

    def assign_homework(self, due_date):
        # Some implementation of assign_homework here...

    def grade_assignment(self, assignment_number):
        # Some implementation of grade_assignment here...

intermediate_oop = ProgrammingCourse("Intermediate Object-Oriented Programming")
Object-Oriented Programming ใน Python ระดับกลาง

เมื่อ implement formal interface ไม่ครบถ้วน

class ProgrammingCourse(Course):
    def __init__(self, course_name):
        self.course_name = course_name

    def assign_homework(self, due_date):
        # Some implementation of assign_homework here...

intermediate_oop = ProgrammingCourse("Intermediate Object-Oriented Programming")
 ...
    intermediate_oop = ProgrammingCourse("Intermediate Object-Oriented Programming")
TypeError: Can't instantiate abstract class ProgrammingCourse with abstract method
    grade_assignment
Object-Oriented Programming ใน Python ระดับกลาง

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

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

Preparing Video For Download...