#Nguyên tắc cốt lõi
👉 Client không nên bị ép phụ thuộc vào những method mà nó không dùng
👉 Rule:
- Interface phải nhỏ, focused
#Bad Example (Fat Interface)
interface WorkerInterface
{
public function work(): void;
public function eat(): void;
public function sleep(): void;
}
#Vấn đề
- Class bị ép implement method không cần
- Throw exception → smell design
- Vi phạm ISP
#Good Example (Segregation)
interface Workable { public function work(): void; }
interface Eatable { public function eat(): void; }
👉 Class chỉ implement cái nó cần
#Giải thích sâu (Senior mindset)
#1. Fat interface là gì?
👉 Interface chứa quá nhiều method
#2. ISP vs SRP
- SRP: class
- ISP: interface
#3. Interface composition
👉 Combine nhiều interface nhỏ
#4. Real-world Laravel
ReadableRepository
WritableRepository
#5. Intersection type (PHP 8.1)
ReadableRepository&PaginatableRepository
#Tips & Tricks (Senior level)
#1. Prefer small interfaces
#2. Avoid “God interface”
#3. Design by use-case
#4. Combine với DIP
#5. Refactor dần
#Interview Questions
1. ISP là gì?
Summary:
- Interface nhỏ, focused
Deep: Không ép class implement method không dùng
2. Fat interface gây vấn đề gì?
Summary:
- Code smell
Deep: Khó maintain, nhiều method dư
3. ISP khác SRP như thế nào?
Summary:
- SRP: class
- ISP: interface
4. Khi nào nên tách interface?
Summary:
- Khi class không dùng hết method
5. ISP liên quan DIP không?
Summary:
- Có
Deep: Interface nhỏ giúp dependency rõ ràng
#Kết luận
👉 ISP giúp:
- Code sạch
- Dễ test
- Dễ mở rộng
👉 Tránh:
- Fat interface