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:

Do your career a favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Top comments (0)

Visualizing Promises and Async/Await 🀯

async await

Learn the ins and outs of Promises and Async/Await!

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay