This website collects cookies to deliver better user experience
Interface Segregation Principle
Interface Segregation Principle
The Interface Segregation Principle (ISP) states:
A client should not implement interfaces that are not necessary for the application.
In other words, it's preferably that instead of one big interface, the developer creates smaller more manageable interfaces.
Let's say we're tasked to create a game about robots, at first, we design our interface:
publicinterfaceIRobot{string Name {get;set;}voidWalk();voidFly();voidAttack();}
Perfect! We created our contract for what makes a robot... that violates the Interface Segregation Principle, because not all robots can Walk, Fly or Attack.
To solve this issue, we need to break down into smaller interfaces:
publicinterfaceIRobot{string Name {get;set;}}publicinterfaceIWalkable{voidWalk();}publicinterfaceIFlyable{voidFly();}publicinterfaceIAggressive{voidAttack();}
Now we can create a Robot the way we want, without implementing extra code that's not necessary, so our FriendlyRobot will be implemented in a different way:
publicclassFriendlyRobot:IRobot,IWalkable{publicstring Name {get;set;}publicvoidWalk(){// Code}}