- SOLID原則(SRP, OCP, LSP, ISP, DIP)
- SRP(Single responsibility,單一職責)
- 定義:一個class應該只有一個需要改變的原因。There should never be more than one reason for a class to change.
- 白話:一個class只作一件事。
- OCP(Open-closed,開放封閉)
- 定義:軟體設計,應該對擴充開放,對修改封閉。Software entities like classes, modules and functions should be open for extension but closed for modifications.
- 白話:軟體要很容易擴充功能,且擴充時原有的code都不需修改。
- LSP(Liskov substitution,Liskov替換)
- 定義:子類必須能夠替換其父類別。Inheritance should ensure that any property proved about supertype objects also holds for subtype objects.
- 白話:設計父類別時,只把所有的子類都有的東西放進來。
- ISP(Interface segregation,介面隔離)
- 定義:客戶(Client)只要依賴它們所需的介面Clients should not be forced to depend upon interfaces that they don't use.
- 白話:設計介面也盡量簡單,別把不相關的東西放進來。
- DIP(Dependency inversion,依賴倒轉)
- 定義:
- 高階模組不應依賴低階模組,兩者應依賴抽象概念。High-level modules should not depend on low-level modules. Both should depend on abstractions.
- 抽象概念不應依賴細節,細節應依賴抽象概念。Abstractions should not depend on details. Details should depend on abstractions.
- 白話:不要具體的指明物件的關係,而要抽象觀念替代之。
2016/1/11
程式設計常見的幾個原則整理
訂閱:
文章 (Atom)