# Blog 2|設計模式 - SOLID
`SOLID(單一功能、開閉原則、里氏替換、介面隔離、依賴反轉)`
設計模式其實是一組解決特定問題的經驗法則和最佳實踐。
避免重複造輪子。
## Single-responsibility principle (SRP) 單一功能
- `A module should have one, and only one, reason to change.`
- 每個類別或模組應該只負責一件事
- https://www.jyt0532.com/2020/03/18/srp/
- https://medium.com/@ChunYeung/%E4%BD%BF%E4%BA%BA%E7%98%8B%E7%8B%82%E7%9A%84-solid-%E5%8E%9F%E5%89%87-%E5%96%AE%E4%B8%80%E8%81%B7%E8%B2%AC%E5%8E%9F%E5%89%87-single-responsibility-principle-c2c4bd9b4e79
- https://shengyu7697.github.io/single-responsibility-principle/
## Open–closed principle (OCP) 開閉原則
- `Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.`
- 一個軟體製品在面對擴展時是開放的,且擴充時不應修改到原有的程式。
- https://www.jyt0532.com/2020/03/19/ocp/
- https://medium.com/@ChunYeung/%E4%BD%BF%E4%BA%BA%E7%98%8B%E7%8B%82%E7%9A%84-solid-%E5%8E%9F%E5%89%87-%E9%96%8B%E6%94%BE%E5%B0%81%E9%96%89%E5%8E%9F%E5%89%87-open-closed-principle-f7eaf921eb9c
- https://shengyu7697.github.io/open-closed-principle/
## Liskov substitution principle (LSP) 里氏替換
- `If S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of the program (correctness, task performed, etc.)`
- 所有的子類別應該可以替換掉它們的父類別,而不會影響程式的正確性
- **大多數你使用繼承的地方都不該用繼承**
- https://www.jyt0532.com/2020/03/22/lsp/
- https://medium.com/@ChunYeung/%E4%BD%BF%E4%BA%BA%E7%98%8B%E7%8B%82%E7%9A%84-solid-%E5%8E%9F%E5%89%87-%E9%87%8C%E6%B0%8F%E6%9B%BF%E6%8F%9B%E5%8E%9F%E5%89%87-liskov-substitution-principle-e66659344aed
- https://shengyu7697.github.io/liskov-substitution-principle/
## Interface segregation principle (ISP) 介面隔離
- `No client should be forced to depend on methods it does not use.`
- 不要強迫一個類別實作它不需要的介面方法
- https://www.jyt0532.com/2020/03/23/isp/
- https://medium.com/@ChunYeung/%E4%BD%BF%E4%BA%BA%E7%98%8B%E7%8B%82%E7%9A%84-solid-%E5%8E%9F%E5%89%87-%E4%BB%8B%E9%9D%A2%E9%9A%94%E9%9B%A2%E5%8E%9F%E5%89%87-interface-segregation-principle-50f54473c79e
- https://shengyu7697.github.io/interface-segregation-principle/
## Dependency inversion principle (DIP) 依賴反轉
- `1.High-level modules should not depend on low-level modules. Both should depend on abstractions.`
- `2.Abstractions should not depend on details. Details should depend on abstractions.`
- 1.高層模組不依賴低層模組,兩者都應依賴抽象介面
- 2.應依賴抽象,不應該依賴實作
- https://www.jyt0532.com/2020/03/24/dip/
- https://medium.com/@ChunYeung/%E4%BD%BF%E4%BA%BA%E7%98%8B%E7%8B%82%E7%9A%84-solid-%E5%8E%9F%E5%89%87-%E4%BE%9D%E8%B3%B4%E5%8F%8D%E5%90%91%E5%8E%9F%E5%89%87-dependency-inversion-principle-a74ca045d776
- https://shengyu7697.github.io/dependency-inversion-principle/