---
# System prepended metadata

title: SOLID
tags: [Design Pattern]

---

---
tags: Design Pattern
---

# SOLID

It is the five basic principles of object-oriented programming proposed by Robert C. Martin.


### <font color="#f00">1. Single-responsiblity principle</font>

一個class應該只需要專注在一件事情上，減少耦合。

A class should only need to focus on one thing, reducing coupling.

### <font color="#f00">2. Open-closed principle
</font>

當在擴增功能的時候，需要在不更改原本程式碼的情況下進行擴增，降低耦合程度，並且程式碼在修改的時候比較方便。

When expanding functions, it is necessary to expand without changing the original code to reduce the degree of coupling, and it is more convenient to modify the code.

### <font color="#f00">3. Liskov substitution principle</font>

若是使用繼承，子類別實作的行為必須要與父類別或是介面所定義的行為一致，並且子類別要能夠完全取代掉父類別，避免呼叫某項功能時，初先與預期不符的功能。

If inheritance is used, the behavior implemented by the subclass must be consistent with the behavior defined by the parent class or interface, and the subclass must be able to completely replace the parent class to avoid calling a function that does not meet the expected function at first.

### <font color="#f00">4. Interface segregation principle</font>

這個原則在講述設計界麵食不應該放入不相關的method，例如在一個叫animal的abstract class內含有run()和fly()的method，由狗和鳥繼承，但是狗會跑卻不會飛，這樣fly()的method卻能被狗使用就很奇怪，所以這個原則主要用途是在降低耦合度，減少不當的設計。

This principle tells that the design interface food should not be put into irrelevant methods. For example, in an abstract class called animal, the methods of run() and fly() are inherited by dogs and birds, but dogs can run but not fly. , it is very strange that the method of fly() can be used by dogs, so the main purpose of this principle is to reduce coupling and reduce improper design.

### <font color="#f00">5. Dependency Inversion Principle</font>

高階模組不要依賴低階模組，兩者都必須要依賴抽象模組；抽象不能依賴細節，細節必須依賴抽象。這樣當我們今天必須要改變、更換低階模組時，也不會影響到高階模組的運作。

High-level modules should not depend on low-level modules, both must depend on abstract modules; abstraction cannot depend on details, and details must depend on abstraction.
In this way, when we have to change or replace low-level modules today, it will not affect the operation of high-level modules.

reference: https://wayne265265.pixnet.net/blog/post/114919277-%E3%80%90%E7%AD%86%E8%A8%98%E3%80%91%E7%89%A9%E4%BB%B6%E5%B0%8E%E5%90%91%E8%A8%AD%E8%A8%88-%3A-solid-%E4%BB%8B%E7%B4%B9
