# มารู้จักกับ SOLID กันดีกว่า

💬 ก่อนที่จะออกวิ่งเล่นกัน ผมแนะนำว่ามารู้จักกับ SOLID Design Principles กันก่อนดีกว่าว่ามันคืออะไร และมีอะไรบ้างที่เราควรจะต้องรู้เพื่อใช้ทำความเข้าใจในคอร์สนี้นะฮั๊ฟ

{% embed url="<https://www.youtube.com/watch?v=lQSOwFtYrOU&list=PLUjAn8nwWniiCUZtTOEHPWHw0WxpdH3DX&index=2>" %}

## 🎯 สรุปสั้นๆ

### 👨‍🚀 SOLID Design Principles

คือคำย่อของหลักการออกแบบซอฟต์แวร์พื้นฐาน 5 ตัวตามรายชื่อด้านล่าง เพื่อให้โค้ดที่ออกมา เข้าใจได้ง่าย มีความยืดหยุ่นสูง โดยบุรุษในตำนาน **Robert C.Marin** หรือที่หลายๆคนรู้จักในนาม **Uncle Bob** ที่เป็นกลุ่มบุกเบิกสร้าง **Agile manifesto** ไงล่ะ

* **S** — Single-Responsibility principle
* **O** — Open/Closed principle
* **L** — Liskov Substitution principle
* **I** — Interface Segregation principle
* **D** — Dependency Inversion principle

{% hint style="success" %}
**Cleancoder**\
คือ blog ของหนึ่งในมหาเทพของวงการซอฟต์แวร์ ซึ่งป๋าแกจะมาสอนเรื่องต่างๆโดยเฉพาะการออกแบบซอฟต์แวร์ ซึ่งถ้าใครเคยฟังแกพูดจะรู้เลยครับว่า แกหลุดโลกไปแล้ว (ฮา) ป๋าแกเก่งจริงๆครับลอง search ชื่อแกใน Youtube ดูได้ผม +100 เลย\
[https://blog.cleancoder.com](https://blog.cleancoder.com/)
{% endhint %}

### 👨‍🚀 Coupling

คือคำที่ใช้เรียกเวลาออกแบบโค้ดที่ไม่ดี เพราะ**โค้ดที่เราเขียนมันไปผูกติดกัน**ไว้ ซึ่งพอเราจะแก้ไขอะไรซักอย่างจะเป็นเรื่องยุ่งยากเช่น จะแก้ A แต่ B เรียกใช้ A อยู่เลยทำให้ต้องไปแก้ B ด้วย แล้วพอแก้ B เราก็พบว่า C เรียกใช้ B อยู่เลยทำให้เราต้องแก้ C เป็นทอดๆอะไรทำนองนี้ ซึ่งในการออกแบบเราควรจะทำให้เกิด Coupling ให้น้อยที่สุด หรือเราเรียก Loose coupling นั่นเอง

### 👨‍🚀 Cohesion

คือคำที่ใช้เรียกเวลาออกแบบโค้ดที่ดี เพราโค้ดที่เราเขียนมันจะสอดคล้องไปในทิศทางเดียวกัน เช่น methods ทุกตัวในคลาสเครื่องคิดเลข มีหน้าที่ในการจัดการกับการคำนวณเท่านั้น จะเห็นว่า method พวกนั้นมันมีหน้าที่สอดคล้องกับคลาสเครื่องคิดเลขที่เอาไวสำหรับคำนวณ แต่ถ้าเครื่องคิดเลขดันมี method ชงกาแฟ ไรงี้จะเห็นว่าถ้าเราจะไปใช้งานเราก็คงจะ งง ว่าจะเรียกใช้ให้มันชงกาแฟเมื่อไหร่ และถ้าจะแก้ไข method นั้นบางทีเราอาจะหามันไม่เจอก็ได้

{% hint style="success" %}
**Coupling & Cohesion**\
หลักพื้นฐานในการออกแบบซอฟต์แวร์คือ ลดการเกิด coupling และเพิ่ม Cohesion ให้ได้มากที่สุด
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.saladpuk.com/basic/solid/intro.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
