Hướng dẫn dùng python notimplementederror python

Bài viết này bàn về hướng lập trình giao diện [programming to the interface] thay vì giao diện người dùng, hay giao diện đồ họa [user interface, graphical interface].

Python không có sự phân biệt giữa ba khái niệm này. Tuy nhiên, trong thực tế, chúng ta hay viết mã tương tự như sau để mô phỏng một giao diện.

class Interface[object]:
    def method_1[self]:
        raise NotImplementedError[]
    def method_2[self]:
        raise NotImplementedError[]

Các phương thức trong lớp Interface chỉ có một câu lệnh duy nhất là raise NotImplementedError[] để cố tình gây ra biệt lệ khi chúng được gọi.

Một lớp trừu tượng có thể được mô phỏng như sau:

class AbstractClass[Interface]:
    def method_1[self]:
        # làm một việc gì đó
        pass

Lớp AbstractClass là lớp con của Interface và hiện thực hóa phương thức method_1 nhưng vẫn để phương thức method_2 lơ lửng.

Cuối cùng, một lớp cụ thể là một lớp con của Interface hoặc AbstractClass nhưng hiện thực hóa tất cả các phương thức.

class ConcreteClass[AbstractClass]:
    def method_2[self]:
        # làm một việc gì đó
        pass

Điểm đáng lưu ý là tất cả các lớp này đều hợp lệ trong Python. Chúng ta hoàn toàn có thể khởi tạo một đối tượng của một trong ba lớp này và chỉ đến khi gọi một hàm nào đó thì biệt lệ mới xảy ra. Nói cách khác, biệt lệ NotImplementedError chỉ được phát hiện khi chạy, trong khi đáng lẽ nó đã phải được phát hiện ngay khi dịch. Điều này làm mất giá trị của một hợp đồng, và hạn chế việc áp dụng hướng lập trình giao diện trong Python.

Để khắc phục điểm yếu này, tôi đã viết một bộ trang hoàng [decorator] nhỏ để bắt lỗi ngay khi một lớp cụ thể được định nghĩa thiếu. Định nghĩa thiếu ở đây mang ý nghĩa rằng lớp cụ thể đã quên định nghĩa một phương thức nào đó.

'''Set of [class] decorators to help with interface programming.

Examples::

  @concrete
  class subclass[parent]:
      ...

Released to the public domain.

Nam T. Nguyen, 2012

'''

import dis
import types
import unittest


def concrete[orig_class]:
    '''A decorator to ensure that a concrete class has no un-implemented
    methods.

    An un-implemented method is defined similarly to::

        def method[...]:
            raise NotImplementedError[]

    This, when translated to bytecode, looks like::

        LOAD_GLOBAL       0 [NotImplementedError]
        CALL_FUNCTION     1
        RAISE_VARARGS     1
        ...

    Or when ``raise NotImplementedError``::

        LOAD_GLOBAL       0 [NotImplementedError]
        RAISE_VARARGS     1
        ...

    The check here is for such pattern.

    '''

    for name in dir[orig_class]:
        func = getattr[orig_class, name]
        # correct type?
        if type[func] not in [types.FunctionType, types.MethodType]:
            continue
        # check if first name is NotImplementedError
        if len[func.func_code.co_names] < 1 or \
                func.func_code.co_names[0] != 'NotImplementedError':
            continue
        # and RAISE_VARARGS somewhere after that
        for position in [3, 6]:
            if len[func.func_code.co_code] < position:
                continue
            opcode = ord[func.func_code.co_code[position]]
            if dis.opname[opcode] == 'RAISE_VARARGS':
                raise SyntaxError['Function %s.%s must be implemented.' % [
                        orig_class.__name__, func.func_name]]

    return orig_class


class ConcreteTest[unittest.TestCase]:

    def test_raise[self]:
        class test1:
            def method[self]:
                raise NotImplementedError
        self.assertRaises[SyntaxError, concrete, test1]

        class test2:
            def method[self]:
                raise NotImplementedError[]
        self.assertRaises[SyntaxError, concrete, test2]

    def test_not_raise[self]:
        class test:
            def method[self]:
                return NotImplementedError[]
        concrete[test]

    def test_subclass[self]:
        class parent[object]:
            def override_me[self]:
                raise NotImplementedError[]
        class test[parent]:
            def leave_it[self]:
                pass
        self.assertRaises[SyntaxError, concrete, test]


if __name__ == '__main__':
    unittest.main[]

Để sử dụng bộ trang hoàng concrete này, ta sẽ sửa lại lớp ConcreteClass như sau:

@concrete
class ConcreteClass[AbstractClass]:
    # như trên

Nếu ConcreteClass quên hiện thực hóa một phương thức nào đó, Python sẽ thông báo SyntaxError ngay sau khi ConcreteClass được định nghĩa.

Hôm nay mình sẽ đi vào tổng quan khái niệm, sau đó lấy ví dụ bằng Python cho các con vợ dễ hiểu nhé =]]

Bài viết gốc trên blog cá nhân của mình: //phamduyhieu.com/solid-trong-oop-va-vi-du-de-hieu-bang-python

1. Single Responsibility
  • Mỗi class chỉ nên có 1 trách nhiệm duy nhất
  • Về lâu dài, nếu không áp dụng S, class sẽ phình to ra, khó kiểm soát và maintain.
# violate the Single Responsibility Principle

class Animal:
    def __init__[self, name]:
        self.name = name

    def get_name[self]:
        pass

    def save_to_db[self, animal]:
        # save to MySQL
        pass


# comply with Single Responsibility Principle

class Animal:
    def __init__[self, name]:
        self.name = name

    def get_name[self]:
        pass


class AnimalDB:
    def get_animal[self, a_id]:
        pass

    def save_to_db[self, animal]:
        pass

Ví dụ ta có class Animal, trong đó gồm cả method lưu object vào database - save_to_db. Với thiết kế này, khi chương trình thay đổi database, ta phải mò vào sửa class gốc này. Thay vào đó ta tạo thêm 1 class nhỏ dành riêng cho việc lưu trữ database là AnimalDB. Việc này giúp cho việc sửa chữa đơn giản, rõ ràng hơn, ít bug hơn.

2. Open Close Principle
  • Nên mở rộng class thay vì sửa đổi class gốc

  • Khi thêm chức năng, … ta nên mở rộng class cũ [kế thừa, sở hữu] mà tránh việc sửa nó

    => dễ gây lỗi tiềm ẩn khi các module khác đang sử dụng class cũ.

class Discount:
    def __init__[self, customer, price]:
        self.customer = customer
        self.price = price

    def get_discount[self]:
        return self.price * 0.2
        
# when we need to add discount for VIP customers => change Discount class

class Discount:
    def __init__[self, customer, price]:
        self.customer = customer
        self.price = price

    def give_discount[self]:
        if self.customer == 'fav':
            return self.price * 0.2
        if self.customer == 'vip':
            return self.price * 0.4

Thiết kế trên đã vi phạm nguyên lý OCP, giả dụ mỗi tuần có thêm 1 case khách hàng mới, chúng ta lại phải sửa class Discount, logic hàm give_discount sẽ dài ra vô tận

Thay vào đó, ta nên tạo 1 class mới kế thừa class cũ

class Discount:
    def __init__[self, customer, price]:
        self.customer = customer
        self.price = price

    def get_discount[self]:
        return self.price * 0.2


class VIPDiscount[Discount]:
    def get_discount[self]:
        return super[].get_discount[] * 1.2


class SuperVIPDiscount[Discount]:
    def get_discount[self]:
        return super[].get_discount[] * 1.5


class DiamondDiscount[Discount]:
    def get_discount[self]:
        return super[].get_discount[] * 2

3. Liskov Substitution Principle
  • Các class con có thể thay thế class cha mà không làm thay đổi tính đúng đắn của chương trình
  • Đảm bảo tính đa hình trong OOP

VD: viết chương trình mô tả các loài chim bay

Có class chimcanhcut cũng là chim nên cho kế thừa class Bird

=> Khi gọi hàm bay của object chim cánh cụt sẽ bị Exception

=> thiết kế này vi phạm nguyên lý LSP

Nôm na khi thiết kế class phải chú ý, tránh bê nguyên các mối quan hệ của các object ngoài đời sống vào code. A là B không có nghĩa là A nên kế thừa B [nếu class A không thể thay thế được class B]

class Animal:
    def leg_count[self]:
        pass


class Lion[Animal]:
    def leg_count[self]:
        pass


def animal_leg_count[animal: Animal]:
    print[animal.leg_count[]]


animal = Animal[]
lion = Lion[]
animal_leg_count[animal]
animal_leg_count[lion]


Với thiết kế bên trên, object lion có thể thay thế object animal từ class Animal mà chương trình vẫn chạy đúng.

4. Interface segregation Principle

• Nên tách interface thành các interface nhỏ hơn phục vụ cho những mục đích cụ thể

# violate Interface segregation
class MyInterface:
    
    def connect_to_db[self]:
        raise NotImplementedError
    
    def write[self]:
        raise NotImplementedError
    
    def read[self]:
        raise NotImplementedError
    
    def close_connect[self]:
        raise NotImplementedError
    
    def show_info[self]:
        raise NotImplementedError
    
    def update_info[self]:
        raise NotImplementedError

# comply with Interface segregation
class DBInterface:

    def connect_to_db[self]:
        raise NotImplementedError

    def write[self]:
        raise NotImplementedError

    def read[self]:
        raise NotImplementedError

    def close_connect[self]:
        raise NotImplementedError


class DisplayInterface:

    def show_info[self]:
        raise NotImplementedError

    def update_info[self]:
        raise NotImplementedError

Với Interface MyInterface, các class khi implement MyInterface sẽ phải implement tất cả các method trong nó. Điều này thành ra bất hợp lý, đôi khi gây dư thừa vì 1 class đôi khi không dùng hết tất cả các method. Vì vậy ta nên chia thành các interface nhỏ [DBInterface, DisplayInterface] gồm các method liên quan đến nhau, dễ quản lý, dễ implement hơn.

5. Dependency Inversion Principle
  • Các module cấp cao không nên phụ thuộc vào các module cấp thấp, cả hai nên phụ thuộc vào abstraction
  • Ta có thể thoải mái sửa đổi implement của module cấp thấp mà không làm ảnh hưởng tới module cấp cao
  • Trong code thực tế, các module nên liên kết với nhau thông qua interface
class IFood:
    def bake[self]:
        raise NotImplemented

    def eat[self]:
        raise NotImplemented


class Pizza[IFood]:
    def bake[self]:
        print["pizza was baked"]

    def eat[self]:
        print["pizza was ate"]


class Bread[IFood]:
    def bake[self]:
        print["bread was baked"]

    def eat[self]:
        print["bread was ate"]


class Production:
    def __init__[self, food: IFood]:
        self.food = food

    def produce[self]:
        self.food.bake[]

    def consume[self]:
        self.food.eat[]


if __name__ == '__main__':
    pizza = Pizza[]
    bread = Bread[]
    
    p = Production[pizza]
    p.produce[]
    p.consume[]
    
    b = Production[bread]
    b.produce[]
    b.consume[]

Ở đây ta có các module cấp thấp là Bread và Pizza, module cấp cao là Production. 2 module này giao tiếp với nhau bằng interface IFood, giúp cho chương trình trở lên linh hoạt hơn. Module Production chỉ cần sử dụng các method trong IFood mà không bị ràng buộc hay cần quan tâm object nào sẽ được truyền vào. Ta có thể truyền vào pizza hoặc bread.

Chủ Đề