The Interface Segregation Principle (ISP) is a SOLID principle that advocates for clients not being forced to depend on interfaces they don't use. This article explains ISP's importance in creating modular, maintainable, and scalable systems by preventing fat interfaces and promoting granular contracts.
Read original on Medium #system-designThe Interface Segregation Principle (ISP) states that clients should not be forced to depend on interfaces they do not use. In simpler terms, it's better to have many small, specific interfaces rather than one large, 'fat' interface. This principle aims to prevent architectural rigidities where changes to an unused part of a large interface could force recompilations or redeployments on unrelated client modules.
Consequences of Fat Interfaces
Fat interfaces introduce unnecessary dependencies. When a client implements a large interface, it becomes coupled to all methods within that interface, even those it doesn't need. This leads to code that is harder to understand, test, and maintain, increasing the risk of bugs and making refactoring difficult.
While primarily discussed in object-oriented programming contexts, ISP has significant implications for system design, especially in microservices architectures and distributed systems. When designing APIs or service contracts, applying ISP means creating granular service interfaces rather than monolithic ones. Each microservice or API endpoint should expose only the functionality relevant to its specific consumers, minimizing coupling and improving independent deployability.
interface PaymentProcessorService {
void processCreditCard(PaymentDetails details);
void processPayPal(PaymentDetails details);
void processCrypto(PaymentDetails details);
}
// ISP Compliant
interface CreditCardProcessor {
void processCreditCard(PaymentDetails details);
}
interface PayPalProcessor {
void processPayPal(PaymentDetails details);
}
interface CryptoProcessor {
void processCrypto(PaymentDetails details);
}