Back to: Java Design Patterns
Interface Segregation Principle (ISP) in Java
In this article, I am going to discuss Interface Segregation Principle (ISP) in Java with Examples. Please read our previous article where we discussed the Liskov Substitution Principle (LSP) in Java. The letter I in the SOLID Design Principle stands for Interface Segregation Principle which is also known as ISP. As part of this article, we are going to discuss the following pointers.
Interface Segregation Principle (ISP) in Java
The Interface Segregation Principle (ISP) is another key principle in the SOLID acronym. It advocates that clients should not be forced to depend on interfaces they do not use. This means that we should design fine-grained interfaces tailored to specific client requirements, rather than having large, monolithic interfaces.
The Interface Segregation Principle states that Clients should not be forced to implement any methods they don’t use. Rather than one fat interface, numerous little interfaces are preferred based on groups of methods with each interface serving one submodule. Let us break down this definition into two parts.
- First, no class should be forced to implement any method(s) of an interface they don’t use.
- Secondly, instead of creating large or you can say fat interfaces, create multiple smaller interfaces with the aim that the clients should only think about the methods that are of interest to them.
By adhering to the ISP, we can prevent clients from being burdened with unnecessary dependencies. When a client depends on an interface that includes methods it does not use, it becomes coupled to those methods. This means that changes to those methods can impact the client, even if it does not use them.
To avoid this issue, we should design our interfaces to be small and focused. Each interface should provide only the methods that are required by a specific client. This reduces the coupling between the client and the interface and minimizes the impact of changes in the interface on the client.
Example to Understand Interface Segregation Principle (ISP) in Java
For example, consider a system that manages user accounts. Instead of having a single large interface with methods for creating, updating, deleting, and retrieving user accounts, we could have separate interfaces for each of these tasks. A client that only needs to retrieve user accounts would only depend on the interface for retrieving user accounts and would not be impacted by changes to the other interfaces.
In this example, we have an interface called a Printer that defines four methods: print(), scan(), fax(), and copy(). However, this violates the Interface Segregation Principle.
public interface Printer { void print(); void scan(); void fax(); void copy(); }
Disadvantages of NOT using the Interface Segregation Principle:
- Client Forced to Implement Unnecessary Methods: Clients that want to implement the Printer interface are forced to provide implementations for all four methods, even if they don’t need all of them. This leads to unnecessary code bloat and potential unused methods in client implementations.
- Violation of Single Responsibility Principle: By providing a single interface with multiple methods, each representing a different functionality, the interface becomes responsible for more than one behavior. This violates the principle of having interfaces with single, focused responsibilities.
- Interface Pollution: Interfaces that contain unrelated methods can confuse clients and make the codebase more complex. Clients may need to implement methods they don’t require, resulting in a larger interface surface area and increased coupling.
To adhere to the Interface Segregation Principle, we can split the Printer interface into smaller, more focused interfaces:
public interface Printer { void print(); } public interface Scanner { void scan(); } public interface FaxMachine { void fax(); } public interface Copier { void copy(); }
In this refactored example, we have segregated the functionality into separate interfaces: Printer, Scanner, FaxMachine, and Copier. Each interface represents a single responsibility.
By following the Interface Segregation Principle, clients can implement only the interfaces that are relevant to their needs. They are no longer forced to provide implementations for methods they don’t require. This promotes a cleaner, more modular design, improves code maintainability, and avoids unnecessary coupling between unrelated functionalities. Clients can depend on the specific interfaces they need, promoting loose coupling and flexibility.
Overall, the Interface Segregation Principle is an important guideline for designing modular and maintainable software. By designing fine-grained interfaces tailored to specific client requirements, we can reduce coupling and minimize the impact of changes in our code.
Advantages of Interface Segregation Principle (ISP) in Java:
The followings are the advantages of using the Interface Segregation Principle in Java
- Reduced Coupling: By segregating interfaces into smaller, more focused ones, the ISP reduces the coupling between classes. Clients only need to depend on the specific interfaces that are relevant to their needs. This leads to looser coupling, which improves the maintainability, flexibility, and extensibility of the system.
- Improved Modularity: The ISP promotes modularity by dividing interfaces based on the functionalities they represent. This allows for better organization and separation of concerns, making the codebase more manageable. Modules and classes become more self-contained, and changes in one interface or module have fewer ripple effects on other parts of the system.
- Enhanced Testability: The ISP contributes to improved testability. With smaller, focused interfaces, it becomes easier to write targeted unit tests for individual methods or groups of related methods. Testing can be more fine-grained and isolated, leading to better coverage and more effective identification of issues or regressions.
- Better Code Understanding: Smaller interfaces resulting from the ISP lead to improved code understanding. Each interface becomes more focused and represents a specific behavior or capability. Developers can quickly grasp the functionality provided by an interface without having to sift through unnecessary methods. This improves the readability, maintainability, and overall comprehension of the codebase.
Disadvantages of Interface Segregation Principle (ISP) in Java:
The followings are the disadvantages of using the Interface Segregation Principle in Java
- Increased Complexity: The ISP can introduce additional complexity, particularly when dealing with a large number of interfaces. Having numerous smaller interfaces may require additional effort to manage and navigate through the codebase. Developers need to strike a balance between interface granularity and the overall complexity of the system.
- Design Overhead: Adhering strictly to the ISP may require more upfront design effort. Analyzing and decomposing interfaces into smaller ones can be time-consuming and challenging, especially in complex systems. Careful consideration and planning are necessary to identify the appropriate level of granularity for the interfaces while avoiding over-engineering.
- Potential Duplication: Splitting interfaces based on specific functionalities may result in duplicated methods across different interfaces. This can lead to redundant code and maintenance overhead if changes need to be applied to multiple interfaces. Efforts should be made to identify common methods and promote code reuse to mitigate this disadvantage.
- Increased Dependency Management: The ISP can lead to an increased number of dependencies between classes and modules. Each client class that uses a specific interface must explicitly depend on it. This can require additional attention to managing and resolving dependencies effectively, especially in larger systems.
In the next article, I am going to discuss Dependency Inversion Principle (DIP) in Java with Examples. Here, in this article, I try to explain Interface Segregation Principle (ISP) in Java with Examples. I hope you enjoy this Interface Segregation Principle (ISP) in the Java article.