I Interface Segregation Principle ISP
2 min readI — Interface Segregation Principle (ISP)
“Clients should not be forced to depend on methods they do not use.”
❌ Bad example:
public interface ITradingPlatform
{
void ExecuteOrder(Order order);
void StreamMarketData();
void SendNotification();
}
Each implementation is forced to implement everything, even if it doesn’t need to.
âś… Good example:
public interface ITradeExecutor { void ExecuteOrder(Order order); }
public interface IMarketDataFeed { void StreamMarketData(); }
public interface INotifier { void SendNotification(); }
đź’ˇ In trading:
IPriceFeedfor market dataITradeExecutorfor executionIRiskServicefor validation
You can plug each service independently into different workflows.
Questions & Answers
A: Narrow interfaces reduce the blast radius of changes. Updating ITradeExecutor doesn’t force unrelated services (like notifications) to recompile or implement dummy methods.
A: Clients implementing “not required” methods or throwing NotSupportedException. Interfaces with dozens of members or mixed responsibilities are prime candidates.
A: External contracts should expose purpose-built endpoints, not monolithic APIs. Clients consume only what they need, reducing coupling and versioning risks.
A: DI allows registering multiple interfaces per class. For example, a service implementing both ITradeExecutor and IRiskService can be resolved through whichever interface the consumer needs.
A: Segregate by cohesive responsibilities, not per method. Keep interfaces meaningful and group operations that change together.
A: Smaller interfaces mean simpler mocks/stubs. Tests focus on the behavior under test without faking unrelated members.
A: Adding methods to a fat interface forces consumers to adapt. With segregated interfaces, you can introduce new interfaces or extension methods without breaking existing ones.
A: Ask “which clients need each member?” and require justification for multi-purpose interfaces. NetArchTest or custom analyzers can flag interfaces exceeding size thresholds.
A: Publish separate events per concern instead of mega-events containing everything. Consumers subscribe only to relevant payloads, mirroring ISP.
A: Clients avoid referencing heavy dependencies they don't use (e.g., price feeds requiring streaming libs). This reduces memory footprint and simplifies deployment.