DEV Community

Vivek Kurmi
Vivek Kurmi

Posted on

Interface Segregation Principle (ISP)

The Interface Segregation Principle (ISP) suggests that you should not force a class to implement interfaces it does not use. In other words, your interfaces should be specific to the needs of the classes that implement them.

Let's explain ISP with a simple code example:

Step 1: Initial Design (Violating ISP)

Imagine you're designing a set of devices that can print and scan documents, but not all devices can do both. You might create a single interface for all devices:

interface MultiFunctionDevice {
    void print();
    void scan();
}
Enter fullscreen mode Exit fullscreen mode

Now, let's create two classes for devices:

class AllInOnePrinter implements MultiFunctionDevice {
    public void print() {
        // Implement print functionality
    }

    public void scan() {
        // Implement scan functionality
    }
}

class Scanner implements MultiFunctionDevice {
    public void print() {
        // This device cannot print, so this method is empty
    }

    public void scan() {
        // Implement scan functionality
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Identify the Problem (Violation of ISP)

The problem here is that the Scanner class is forced to implement a print method even though it cannot print. This violates the Interface Segregation Principle because it's making the Scanner class implement something it doesn't need or use.

Step 3: Apply the Interface Segregation Principle (Solution)

To adhere to ISP, you should create separate interfaces that are specific to each device's capabilities.

Step 4: Refined Design (Adhering to ISP)

Let's create separate interfaces for printing and scanning:

interface Printer {
    void print();
}

interface Scanner {
    void scan();
}
Enter fullscreen mode Exit fullscreen mode

Now, we can implement these interfaces in our device classes without forcing them to implement methods they don't use:

class AllInOnePrinter implements Printer, Scanner {
    public void print() {
        // Implement print functionality
    }

    public void scan() {
        // Implement scan functionality
    }
}

class SimpleScanner implements Scanner {
    public void scan() {
        // Implement scan functionality
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Benefits of the Solution (Adhering to ISP)

By adhering to the Interface Segregation Principle:

  • Classes only implement the methods that are relevant to their functionality, avoiding unnecessary code.
  • Code becomes more maintainable and less error-prone because you don't have to provide empty or meaningless method implementations.
  • It's easier to understand the roles and capabilities of different classes in your code.

In simple terms, the Interface Segregation Principle advises creating specific interfaces for specific tasks to prevent classes from having to implement methods they don't need. This leads to cleaner and more focused code.

"Your feedback and ideas are invaluable – drop a comment, and let's make this even better!"

😍 If you enjoy the content, please πŸ‘ like, πŸ”„ share, and πŸ‘£ follow for more updates!
Join me on a professional journey through my LinkedIn profile: Linkedin Profile

Top comments (0)