Learning Interface Segregation Principle
The Interface Segregation Principle
The Interface Segregation Principle states that no code should be forced to depend on methods it doesn’t use. The idea is to keep interfaces lean and focused while preventing bloat or fat interfaces. Such small, focused interfaces are also known as role interfaces. Implementing fat interfaces can lead to unnecessary code in client classes.
When an interface contains lots of behavior, it’s likely to violate this principle. When this happens, separate some behaviors into their interfaces. This is somewhat similar to what single responsibility does for classes.
Advantages of the Interface Segregation Principle
This principle:
- Promotes loose coupling.
- Enhances maintainability.
- Makes code easy to refactor.
- Is flexible enough to accommodate new changes.
- Ensures that clients always have methods relevant to them.
Disadvantages of the Interface Segregation Principle
Although this principle has several benefits, it can also introduce multiple interfaces that increase overhead, making the code complex and difficult to organize and manage.
In the next demo, you’ll see how to abide by this principle in your e-commerce app.