Interface Segregation Principle (ISP)
Definition
Clients should not be forced to depend on interfaces they do not use.
👉 Prefer small focused interfaces instead of one large interface.
Purpose
- Reduce unnecessary implementation
- Keep interfaces clean
- Improve flexibility
- Avoid unused methods
- Build modular systems
Bad Example ❌
Large Interface
public interface IWorker
{
void Work();
void Eat();
}
Human Worker
public class Human : IWorker
{
public void Work()
{
Console.WriteLine("Human Working");
}
public void Eat()
{
Console.WriteLine("Human Eating");
}
}
Robot Worker
public class Robot : IWorker
{
public void Work()
{
Console.WriteLine("Robot Working");
}
public void Eat()
{
throw new Exception(
"Robot doesn't eat"
);
}
}
Problem
Robot forced to implement:
Eat()
even though it doesn't need it.
👉 Violates ISP.
Good Example ✅
Work Interface
public interface IWorkable
{
void Work();
}
Eat Interface
public interface IEatable
{
void Eat();
}
Human Class
public class Human :
IWorkable,
IEatable
{
public void Work()
{
Console.WriteLine("Human Working");
}
public void Eat()
{
Console.WriteLine("Human Eating");
}
}
Robot Class
public class Robot : IWorkable
{
public void Work()
{
Console.WriteLine("Robot Working");
}
}
Key Concept
Instead of:
One huge interface
We do:
Multiple focused interfaces
👉 Clients implement only what they need.
Real-Time Scenario
Notification System:
- IEmailSender
- ISmsSender
- IPushNotificationSender
👉 Services implement only required functionality.
Advantages
- ✔ Cleaner interfaces
- ✔ Reduced unnecessary code
- ✔ Better flexibility
- ✔ Easier maintenance
- ✔ Better modularity
Disadvantages
- ✖ More interfaces/classes
- ✖ Slight design complexity increase
When to Use
Use ISP when:
- Interfaces become too large
- Classes implement unused methods
- Different clients need different functionality
- System requires modular design
Real Project Mapping (.NET + Angular)
| Feature | ISP Usage |
|---|---|
| Repository interfaces | ISP |
| Notification services | ISP |
| Payment providers | ISP |
| File storage services | ISP |
| API contracts | ISP |
ASP.NET Core Real Example
Repository Interfaces
IReadRepository
IWriteRepository
👉 Separate read/write responsibilities.
Advanced Example – Cloud Storage
IFileUpload
IFileDownload
IFileDelete
👉 Services implement only required operations.
ISP vs SRP
| ISP | SRP |
|---|---|
| Focus on interfaces | Focus on classes |
| Small interfaces | Single responsibility |
| Prevent unused methods | Prevent mixed responsibilities |
Common ISP Violation Signs
- Interfaces with too many methods
- Empty method implementations
- throw new Exception() in methods
- Classes implementing unrelated functionality
Pro Tip
ISP works best with:
- Clean Architecture
- Microservices
- Repository Pattern
- CQRS
- Dependency Injection
Summary
ISP helps you:
- Create small focused interfaces
- Reduce unnecessary dependencies
- Build modular systems
👉 Perfect for:
- Enterprise applications
- APIs
- Microservices
- Service-oriented architectures
Comments
Post a Comment