Interface Segregation Principle (ISP):

SOLID - SRP

The Interface Segregation Principle (ISP) states that clients should not be forced to depend on interfaces they do not use. In other words, it suggests that interfaces should be tailored to specific client needs, rather than having large interfaces that cover all possible scenarios.

Let's consider a practical use case to understand the Interface Segregation Principle:

Use Case: Document Processing System

Example: Suppose we are developing a document processing system that handles different types of documents, such as Word documents, PDFs, and spreadsheets. The system provides various operations like editing, printing, and exporting documents.

# Without applying the ISP:

In a naive implementation, we might have a single DocumentProcessor interface that includes methods for editing, printing, and exporting documents. However, this violates the ISP because clients that only need to perform specific operations, like printing, would still have to depend on the entire interface.

# Applying the ISP:

To apply the ISP, we can design smaller and more focused interfaces that satisfy specific client needs:

Syntax:
                
    public interface Printable {
        void print();
    }

    public interface Editable {
        void edit();
    }

    public interface Exportable {
        void export();
    }

    public class WordDocument implements Printable, Editable, Exportable {
        // implementation of methods for Word documents
    }

    public class PDFDocument implements Printable, Exportable {
        // implementation of methods for PDF documents
    }

    public class Spreadsheet implements Editable, Exportable {
        // implementation of methods for spreadsheets
    }
                
            

In this example, we have separate interfaces (Printable, Editable, Exportable) that represent specific behaviors related to document processing. Each document class like WordDocument, PDFDocument, Spreadsheet implements only the interfaces that are relevant to its functionality.

The WordDocument class implements Printable, Editable, and Exportable interfaces since it supports all those operations. The PDFDocument class implements Printable and Exportable interfaces as PDFs can be printed and exported but not directly edited. The Spreadsheet class implements Editable and Exportable interfaces, as spreadsheets can be edited and exported but don't support direct printing.

By following the ISP, clients can now depend only on the interfaces they need. For example, a printing module would only need to interact with the Printable interface, and an export module would only require the Exportable interface. This modular approach allows for more flexible and decoupled code, as clients are not burdened with unnecessary dependencies.

The Interface Segregation Principle promotes the idea of designing interfaces with a narrow focus, tailoring them to specific client requirements. This ensures that clients are not forced to depend on methods they don't use, resulting in more maintainable, reusable, and cohesive code.