面向对象编程中的 SOLID 原则

  • 单一功能原则/Single-responsibility principle
    • 对象应该尽量功能单/A class should only have a single responsibility, that is, only changes to one part of the software's specification should be able to affect the specification of the class.
  • 开闭原则Open/closed principle
    • 开放扩展封闭修改/"Software entities ... should be open for extension, but closed for modification."
  • 里氏替换原则/Liskov substitution principle
    • 使用子类对象替换后不影响正常功能/"Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program." See also design by contract.
  • 接口隔离原则/Interface segregation principle
    • 多个特定职责的接口好于单个通用接口/"Many client-specific interfaces are better than one general-purpose interface."
  • 依赖反转原则/Dependency inversion principle
    • 依赖于抽象而不是单个实例,依赖注入就是该思想的一种实现/One should "depend upon abstractions, [not] concretions."

相关资源