Table of Content
Single Responsibility Principle (ISP) states
A client should never be forced to implement an interface that it does not use, or clients should not be forced to depend on methods they do not use.
Example with Violation (Invalid)
class Square {
setWidth(width) {}
...
volume() {}
}
class Cube extends Square {
setWidth(width) {}
volume() {}
}
why does it valid ISP?
Any Flat square your create must be implement volume function that it has no use of.
Benefits of ISP
- Reduced coupling: Classes depend on smaller, more specific interfaces, which decreases unnecessary dependencies between classes.
- Improved maintainability: Changes to an interface won't impact classes that don't use the modified methods.
- Better cohesion: Interfaces are tailored to the exact needs of the client classes, promoting clearer responsibilities and a more cohesive design.