DEV Community

Dzung Nguyen
Dzung Nguyen

Posted on

2

Interface Segregation Principle (ISP) Explained in 100 Seconds

πŸ’‘ What is Interface Segregation Principle (ISP)?

The ISP, part of the SOLID principles, states:

"A client should not be forced to depend on methods it does not use."

  • When an interface has too many unrelated methods, classes implementing it may end up with unnecessary or unused code, leading to fragile, hard-to-maintain systems.

  • ISP encourages creating smaller, more specific interfaces to keep things clean and manageable.

🎯 Simple Example

Imagine you’re ordering from a restaurant with a menu full of dishes you don’t like. You have to flip through 30 pages of irrelevant options to find it. Frustrating, right?

What if there were focused, specialized menus β€” one for sandwiches, one for sushi, one for desserts? Suddenly, ordering becomes becomes easier. This is exactly what ISP suggests:

🚫 No massive, one-size-fits-all interfaces full of unused methods.

βœ… Small, specific interfaces tailored to fit the needs of their users.

Menu

Code Example

Code Example 1

Code Example 2

🌟 Benefits

βœ… No more unnecessary methods: Each class gets exactly what it needs, nothing more.

βœ… Easier to understand, maintain and extend.

βœ… Cleaner design: No more empty or "not supported" methods.

⚠️ Signs of Violations

❌ Large Interfaces
❌ Unimplemented Methods

πŸ’‘ How to Avoid ISP Violations

βœ… Refactor Large Interfaces into smaller, more specialized ones.
βœ… Implement Only What’s Needed β€” classes should only implement methods relevant to their behavior.
βœ… Focus on Composition, not inheritance, so your code stays flexible and easier to extend without violating ISP.

πŸ“° Others

Interested? πŸ˜ƒ Check out other posts from my programming principles series!


Follow me to stay updated with my future posts:

Top comments (0)

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

β­οΈπŸŽ€ JavaScript Visualized: Promises & Async/Await

async await