Undesrtanding Interface Segregation Principle
The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. It was introduced by Robert C. Martin in his book “Design Principles and Design Patterns” in 2000.
The principle states:
A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use.
What does it mean?
-
Client-Specific Interfaces: Instead of having a single large interface that contains all the methods, we should create smaller, client-specific interfaces that contain only the methods that a particular client needs. This allows clients to depend only on the methods they use, rather than being forced to implement or depend on methods they don’t need.
-
Interface Segregation: By segregating interfaces into smaller, more focused ones, we can reduce the complexity of the system and make it easier to understand and maintain. This also helps to avoid the problem of “fat” interfaces that contain too many methods.
Benefits of Interface Segregation Principle
-
Flexibility: The Interface Segregation Principle allows for more flexible and modular code. Clients can depend only on the methods they need, which makes the system more adaptable to changes and easier to maintain.
-
Simplicity: By creating smaller, client-specific interfaces, we can simplify the design of the system and make it easier to understand. This reduces the risk of errors and makes the code more readable.
-
Scalability: The Interface Segregation Principle makes the system more scalable. As new clients are added, we can create new interfaces that contain only the methods they need, rather than modifying existing interfaces or creating new ones that contain all the methods.
Example which violates Interface Segregation Principle
Consider a Document class that has a print and scan method. Now, if you have a client that only needs to print documents, it would be forced to implement the scan method, which violates the Interface Segregation Principle.
interface Document {
print(): void;
scan(): void;
}
class Printer implements Document {
print() {
console.log('Printing document');
}
scan() {
throw new Error('Not supported');
}
}
const printer = new Printer();
printer.print();
printer.scan(); // Error: Not supported
Example which follows Interface Segregation Principle
To follow the ISP, we can create separate interfaces for printing and scanning, and have clients depend only on the methods they need.
// Define a Print interface
interface Print {
print(): void;
}
// Define a Scan interface
interface Scan {
scan(): void;
}
// Implement Printer class
class Printer implements Print {
print() {
console.log('Printing document');
}
}
// Implement Scanner class
class Scanner implements Scan {
scan() {
console.log('Scanning document');
}
}
// Usage
const printer = new Printer();
printer.print();
const scanner = new Scanner();
scanner.scan();
By following the Interface Segregation Principle, we have created smaller, client-specific interfaces that contain only the methods that a particular client needs. This makes the system more flexible, simpler, and easier to maintain.
Conclusion
The Interface Segregation Principle is an important principle of object-oriented design that helps to create more flexible, modular, and scalable code. By segregating interfaces into smaller, client-specific ones, we can reduce the complexity of the system and make it easier to understand and maintain. This principle is particularly useful in situations where clients have different needs and should not be forced to depend on methods they do not use.