面向对象设计 SOLID 原则和python例子

面向对象设计 SOLID 原则共有 5 个,它们分别是:

1、依赖倒置原则 (Dependency Inversion Principle, DIP) :指的是高层模块 (high-level modules) 不应该依赖于低层模块 (low-level modules), 两者都应该依赖于抽象 (abstractions)。也就是说要以抽象为基础,而不是以实现为基础来进行编程

我个人的理解是,要先抽象出一个接口类,然后接口不实现具体的实现,但要定义输入的参数,起到让调用的人作为参考的作用。


# 一个例子:

class LowLevelModule:
    def low_level_method(self):
        pass

class HighLevelModule:
    def __init__(self):
        self.low_level_module = LowLevelModule()

    def high_level_method(self):
        self.low_level_module.low_level_method()

# 在上面的代码中,HighLevelModule 类依赖于 LowLevelModule 类。为了遵循 DIP,我们应该将这种依赖关系反转过来:

class LowLevelModule:
    def low_level_method(self):
        pass

class HighLevelModule:
    def __init__(self, low_level_module):
        self.low_level_module = low_level_module

    def high_level_method(self):
        self.low_level_module.low_level_method()

# 在这个例子中,HighLevelModule 类不再依赖于具体的实现类 LowLevelModule,而是依赖于抽象(接口)。

2、里氏替换原则 (Liskov Substitution Principle, LSP) :指的是如果一个程序中使用的是基类 (base class) 的对象,那么在不改变程序正确性的前提下,这个程序中基类对象的地方都可以使用其子类 (subclass) 对象来替换

我个人的理解是,要能够像调用父类一样调用子类,例如父类有一个方法是怎么调用的,子类也要有这个方法,并且是一样的调用这个方法


# 以下是一个例子:

class Shape:
    def area(self):
        pass

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

class Square(Rectangle):
    def __init__(self, side):
        self.width = side
        self.height = side

def calculate_area(shape: Shape) -> float:
    return shape.area()

rectangle = Rectangle(2, 3)
print(calculate_area(rectangle))  # 6

square = Square(2)
print(calculate_area(square))  # 4
# 在上面的代码中,我们定义了一个 Shape 类和两个子类 Rectangle 和 Square。
# 这两个子类都可以替换成基类 Shape,并且都可以正常工作。

# 在函数 calculate_area 中,我们传入了一个 Shape 类型的参数,这个函数可以处理任何继承
# 自 Shape 的子类,而不需要考虑具体的子类是什么,这就是里氏替换原则的体现。

# 里氏替换原则的要求是,子类需要满足的条件是,不破坏父类的约束条件,并且满足自己的约束条件。

3、开放封闭原则 (Open-Closed Principle, OCP) :指的是一个类或模块应该对扩展开放,对修改封闭。也就是说,当需要新增功能时,应该通过增加新的代码来实现,而不是修改现有的代码。

我个人的理解是:由于一个类可能会被多个地方都使用到,如果随便对这个类进行修改,那么可能很多引用这个类的地方都需要进行修改,这个工作量是很大的,而且还容易没有全部地方都进行修改,从而引发 BUG。因此可以选择对这个类进行继承,将新功能在继承的子类里进行实现。也可以写一个名字跟原来的接口的名字不同的新接口。个人感觉重新写一个新名字的接口的方法更简单,但是如果要增加的新接口很多的话,还是使用子类继承更好。


以下是一个例子:

class Shape:
    def area(self):
        pass

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius ** 2

class ShapeCalculator:
    def calculate_area(self, shape):
        return shape.area()
    
calculator = ShapeCalculator()
rectangle = Rectangle(2, 3)
print(calculator.calculate_area(rectangle)) # 6
circle = Circle(2)
print(calculator.calculate_area(circle)) # 12.56

# 在上面的代码中,我们定义了一个基类 Shape, 两个子类 Rectangle 和 Circle。
# 在类 ShapeCalculator 中,我们有一个函数 calculate_area,该函数可以处理任何继承
# 自 Shape 的子类,而不需要考虑具体的子类是什么,这样我们可以在不修改现有代码的情况
# 下新增新的图形类型,这就是开放封闭原则的体现。

# 这种设计方式可以保证现有代码不会因新增功能而改变,从而降低维护代码的成本。

4、接口隔离原则 (Interface Segregation Principle, ISP) :指的是客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上。

我个人的理解是:假如一个接口类有很多的接口,但是实现这个接口类的具体实现类,其实有很多接口是使用不到的,因此如果要一一实现这些接口,既无法具体实现,也没有必要耗费大量的时间精力去实现这些接口,因此需要将接口类尽量分成多个接口类,每个接口类都比较精简,这样在实现的时候,就可以避免要去实现它不需要实现的接口。


# 以下是一个例子:

class Document:
    def __init__(self, content):
        self.content = content

    def save(self):
        pass

    def print(self):
        pass

    def fax(self):
        pass

class SimplePrinter:
    def print(self, document: Document):
        print(document.content)

class Fax:
    def fax(self, document: Document):
        print(f"Faxing the document: {document.content}")

# 在上面的代码中,我们定义了一个Document类,它包含三个方法:save,print,fax。
# 然而,并不是所有的客户端都需要使用这三个方法,比如 SimplePrinter只需要使用
#  print 方法,而不需要使用 save, fax 方法,这样就导致了客户端不必要的依赖。

# 为了遵循 ISP,我们应该将这些接口拆分为更小的接口,比如:

class PrintableDocument(ABC):
    @abstractmethod
    def print(self):
        pass

class SaveableDocument(ABC):
    @abstractmethod
    def save(self):
        pass

class FaxableDocument(ABC):
    @abstractmethod
    def fax(self):
        pass

class Document(PrintableDocument, SaveableDocument, FaxableDocument):
    def __init__(self, content):
        self.content = content

    def save(self):
        pass

    def print(self):
        print(self.content)

    def fax(self):
        pass

class SimplePrinter:
    def print(self, document: PrintableDocument):
        print(document.content)

class Fax:
    def fax(self, document: FaxableDocument):
        print(f"Faxing the document: {document.content}")
# 这样,我们就将 Document 类拆分成了三个更小的接口

5、单一职责原则:单一职责原则 (Single Responsibility Principle, SRP) :指的是一个类应该只有一个引起它变化的原因。也就是说,一个类只应该有一个负责其中的逻辑。

我个人的理解是:如果一个类需要实现多个职责,那么可能会导致这个类的功能不够明确,在实现时会增加很多不该在这个类中实现的功能。


# 以下是一个例子:

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def increase_salary(self, percent):
        self.salary = self.salary + (self.salary * percent) / 100

    def save_employee(self):
        # save the employee to the database
        pass

    def send_email(self):
        # send email to the employee
        pass
# 在上面的代码中,类 Employee 负责三种职责:薪水计算、存储到数据库和发送电子邮件。

# 为了遵循 SRP,我们应该将这些职责拆分成不同的类,比如:

class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

class SalaryCalculator:
    def increase_salary(self, employee: Employee, percent):
        employee.salary = employee.salary + (employee.salary * percent) / 100

class EmployeeDB:
    def save_employee(self, employee: Employee):
        # save the employee to the database
        pass

class EmailSender:
    def send_email(self, employee: Employee):
        # send email to the employee
        pass

# 这样每个类只负责一个单一的职责,使得类更加简单,容易维护,这就是单一职责原则的体现。

这几个原则之间有没有互相矛盾的地方?

这几个原则之间并不会互相矛盾,而是相互补充和协同工作。

依赖倒置原则 (Dependency Inversion Principle, DIP) 强调的是高层模块不应该依赖低层模块,

而是应该依赖抽象。这个原则可以和单一职责原则 (Single Responsibility Principle, SRP) 协同工作,

因为如果低层模块有多个职责,那么就会增加高层模块的依赖。

里氏替换原则 (Liskov Substitution Principle, LSP) 强调的是子类应该能够替换父类。这个原则可以和

开放封闭原则 (Open-Closed Principle, OCP) 协同工作,因为如果父类是可扩展的,那么子类就可以扩展

父类的行为,而不会破坏程序的正确性。

接口隔离原则 (Interface Segregation Principle, ISP) 强调的是客户端不应该依赖它不需要的接口。

这个原则可以和依赖倒置原则 (Dependency Inversion Principle, DIP) 协同工作,因为如果低层模块的

接口不是客户端需要的,那么就会增加高层模块的依赖。

总之,这些原则是相互补充和协同工作的,它们的目的都是为了让代码更加简单,易于维护和扩展。遵循这些

原则可以使得代码具有高内聚性和低耦合性,这样可以降低程序的复杂性,提高程序的可维护性和可扩展性。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值