The Single Responsibility Principle An active corollary to Conway’s law: The best structure for a software system is heavily influenced by the social structure of the organization that uses it so that each software module has one, and only one, reason to change.

  • 一個 class 只負責一件事。
  • Module 更容易模組化、更容易做單元測試。
  • (實作上的高聚合)


The Open-Closed Principle Bertrand Meyer made this principle famous in the 1980s. The gist is that for software systems to be easy to change, they must be designed to allow the behavior of those systems to be changed by adding new code, rather than changing existing code.

  • 新增新的行為,不用改 code (Close),只需要加 code 就好 (Open)。
  • (寫 base class/interface 的方法)


The Liskov Substitution Principle Barbara Liskov’s famous definition of subtypes, from 1988. In short, this principle says that to build software systems from interchangeable parts, those parts must adhere to a contract that allows those parts to be substituted one for another.

  • 就算是要新增功能,也要忠於原有 interface 的約定。
  • 就算 subclass 的實作再怎麼有變化,你也只是在"實作 interface"。
  • (寫 sub class/implemntation 的方法)


The Interface Segregation Principle This principle advises software designers to avoid depending on things that they don’t use.

  • Interface 的範圍越小越精確越好。
  • (介面上的高聚合)


The Dependency Inversion Principle The code that implements high-level policy should not depend on the code that implements low-level details. Rather, details should depend on policies.

  • Module 都應該只(向上)相依於介面就好,而不是(向下)相依於實作。
  • 上層要更改 policy (傳入不同實作),也不用改到 Module。
  • 差不多就是封裝。

引言內文來自: Clean Architecture: A Craftsman's Guide to Software Structure and Design

comments powered by Disqus