面向对象设计原则

面向对象设计原则0

目的:应对变化、提高复用;寻找变化点;将设计原则提升为设计经验;

单一职责原则(SRP)1

单一职责原则,英文全称Single-responsibility Principle,缩写是SRP。

“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.”

Every class in a computer program should have responsibility over a single part of that program’s functionality, which it should encapsulate. All of that module, class or function’s services should be narrowly aligned with that responsibility.

  • 一个类应该仅有一个引起它变化的原因
  • 变化的方向隐含着类的职责

开放封闭原则(OCP)2

开放封闭原则,又称为开闭原则,英文全称是Open-close Principle,缩写是OCP。

“software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”

That is, such an entity can allow its behavior to be extended without modifying its source code.

  • 对扩展开放,对更改封闭
  • 类模块应该是可扩展的,但是不可修改

里氏替换原则(LSP)3

里式替换原则,又称为Liskov替换原则,英文全称是Liskov Substitution Principle,缩写是LSP。

If S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., an object of type T may be substituted with any object of a subtype S) without altering any of the desirable properties of the program (correctness, task performed, etc.).

  • 子类必须能够替换它们的基类(IS-A)
  • 继承表达类型抽象

接口隔离原则(ISP)4

接口隔离原则,英文全称是Interface Segregation Principle,缩写是ISP。

No client should be forced to depend on methods it does not use

  • 不应该强迫客户程序依赖它们不用的方法
  • 接口应该小而完备

依赖倒置原则(DIP)5

依赖倒置原则,英文全称是Dependence Inversion Principle,缩写是DIP。

  1. High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g., interfaces).
  2. Abstractions should not depend on details. Details (concrete implementations) should depend on abstractions.
  • 高层模板(稳定)不应该依赖于底层模块(变化),二者都应该依赖于抽象(稳定)
  • 抽象(稳定)不应该依赖于实现细节(变化),实现细节应该依赖于抽象(稳定)

优先使用对象组合,而不是类继承

  • 类继承通常为“白箱复用”,对象组合通常为“黑箱复用”
  • 继承在某种程度上破坏了封装性,子类父类耦合度高
  • 而对象组合则只要求被组合的对象具有良好定义的接口,耦合度低

封装变化点

  • 使用封装来创建对象之间的分界层,让设计者可以在分界层的一侧进行修改,而不会对另一侧产生不良的影响,从而实现层次间的松耦合

针对接口编程,而不是针对实现编程

  • 不将变量类型声明为某个特定的具体类,而是声明为某个接口
  • 客户程序无需获知对象的具体类型,只需知道对象所具有的接口
  • 减少系统中各部分的依赖关系,从而实现“高内聚、松耦合”的类型设计方案

软件设计术语

  • 设计习语(Design Idioms):描述与特定编程语言相关的低层模式、技巧、习惯用法。
  • 设计模式(Design Patterns):描述的是类与相互通信的对象之间的组织关系,包括它们的角色、职责、协作方式等方面。
  • 架构模式(Architectural Patterns):描述系统中与基本结构组织关系密切的高层模式,包括子系统划分、职责、以及如何组织它们之间关系的规则。

Reference


1. https://en.wikipedia.org/w/index.php?title=Single-responsibility_principle&oldid=1001170071 Single-responsibility principle
2. https://en.wikipedia.org/w/index.php?title=Open%E2%80%93closed_principle&oldid=1001467540 Open–closed principle
3. https://en.wikipedia.org/w/index.php?title=Liskov_substitution_principle&oldid=1002696695 Liskov substitution principle
4. https://en.wikipedia.org/w/index.php?title=Interface_segregation_principle&oldid=995747843 Interface segregation principle
5. https://en.wikipedia.org/w/index.php?title=Dependency_inversion_principle&oldid=997636861 Dependency inversion principle
6. https://www.bilibili.com/video/BV1kW411P7KS?p=2