Interface Segregation Principle (ISP)
The Interface Segregation Principle (ISP) is another principle of the SOLID acronym, a set of five principles of object-oriented programming and design. The principles make it easier to develop software that is easy to manage and maintain, and also to understand.
The Interface Segregation Principle is a concept in object-oriented programming that can be explained with a simple analogy:
Imagine a multi-purpose remote control with buttons for operating a TV, a sound system, and a gaming console. Now, if you only have a TV, it's unnecessary and confusing to have buttons for the sound system and gaming console. This remote would be much easier to use if it only had the buttons needed for each specific device.
In programming, the Interface Segregation Principle suggests something similar:
Specific Interfaces, Not General: Just like having a remote control tailored for each device, this principle states that classes should not be forced to implement interfaces they do not use. Instead of one big interface, you should have several specific ones.
Smaller, Customized Interfaces: Create small, specific interfaces that are tailored for each class, rather than one large, general-purpose interface. This way, a class will only have to implement methods that are relevant to it.
Avoiding Unnecessary Implementation: It's like not having to put buttons for a gaming console on a remote control meant only for a TV. In programming, a class shouldn't have to implement functions that it doesn't need.
Flexibility and Cohesion: This approach leads to a more flexible and cohesive code structure. It allows you to change and adapt parts of your system without affecting those that don't rely on those interfaces.
So, in simple terms, the Interface Segregation Principle advises that it's better to have several smaller, more specific interfaces than one large, catch-all interface. This makes your code more modular, easier to understand, and avoids unnecessary dependencies.
The Interface Segregation Principle states that clients should not be forced to depend on interfaces they do not use. In other words, it's about making fine-grained interfaces that are client-specific rather than having a single general-purpose interface.
This principle helps to keep a system decoupled and thus easier to refactor, change, and redeploy. It leads to more robust, less error-prone code.
Let's look at a simple example:
Suppose you have a Printer
interface that declares methods print()
, fax()
, and scan()
. If you create a class InkjetPrinter
that implements this interface, you'll have to implement all three methods, even if this printer doesn't support faxing or scanning. This forces the InkjetPrinter
class to depend on methods it doesn't actually use.
According to the Interface Segregation Principle, we should instead have separate interfaces for each function: Printer
, Fax
, and Scanner
. The InkjetPrinter
would implement only the Printer
interface, while a class for a high-end office printer could implement all three interfaces. This way, each class only needs to know about the methods it actually uses.
So, the principle is all about reducing the side effects and frequency of required changes by splitting interfaces that are very large into smaller and more specific ones, so that clients will only have to know about the methods that are of interest to them.
COSC-1437 / ITSE-2457 Computer Science Dept. - Author: Dr. Kevin Roark