SOLID Design Principles: Learn the Interface-Segregation Principle
Tags: python, cleancode, programming, solidprinciples
Published: 2023-11-27 15:16:09

👋 Hey there, fellow software enthusiasts!

I'm Revisto, a passionate software engineer 👨‍💻, and I want to dive into the exciting world of SOLID principles.

**SOLID** is a set of rules and principles that help us create maintainable, reusable, and flexible software designs. These principles guide us in building software that can adapt and grow as our projects evolve. Today, we will focus on the third principle of SOLID.

- S: [Single Responsibility Principle](https://dev.to/revisto/solid-design-principles-learn-the-single-responsibility-principle-3cb1)
- O: [Open-Closed Principle](https://dev.to/revisto/solid-design-principles-learn-the-open-closed-principle-2o8o)
- L: [Liskov Substitution Principle](https://dev.to/revisto/solid-design-principles-learn-the-liskov-substitution-principle-2em9)
- **I: Interface Segregation Principle**
- D: Dependency Inversion Principle



## Understanding the Interface Segregation Principle:
✨ In object-oriented programming, **an interface represents a set of behaviors that an object can perform**. It serves as a contract that defines the methods an object must have. The purpose of interfaces is to **enable clients to interact with objects through a common interface**, without needing to know the intricate details of how those methods are implemented.

The core idea behind the Interface Segregation Principle can be summarized as follows: 

> Clients should not be forced to depend upon methods that they do not use. Interfaces belong to clients, not to hierarchies🤩.



In Python, abstract classes are commonly used as interfaces, aligning with the philosophy of duck typing. The duck typing principle states that:
> If it walks like a duck and quacks like a duck, it must be a duck🦆.

📔 Common Example:


```python
from abc import ABC, abstractmethod

class Printer(ABC):
    @abstractmethod
    def print(self, document):
        pass

    @abstractmethod
    def fax(self, document):
        pass

    @abstractmethod
    def scan(self, document):
        pass

class OldPrinter(Printer):
    def print(self, document):
        print(f"Printing {document} in black and white...")

    def fax(self, document):
        raise NotImplementedError("Fax functionality not supported")

    def scan(self, document):
        raise NotImplementedError("Scan functionality not supported")

```

The ISP suggests that interfaces should be designed in a way that clients are not forced to depend on methods they don't need.

❌ The OldPrinter class implements the print method to print documents in black and white. However, it raises **NotImplementedError** for the **fax** and **scan** methods, indicating that these functionalities are not supported by this particular printer.

```python
from abc import ABC, abstractmethod

class Printer(ABC):
    @abstractmethod
    def print(self, document):
        pass

class Fax(ABC):
    @abstractmethod
    def fax(self, document):
        pass

class Scanner(ABC):
    @abstractmethod
    def scan(self, document):
        pass

class OldPrinter(Printer):
    def print(self, document):
        print(f"Printing {document} in black and white...")

class NewPrinter(Printer, Fax, Scanner):
    def print(self, document):
        print(f"Printing {document} in color...")

    def fax(self, document):
        print(f"Faxing {document}...")

    def scan(self, document):
        print(f"Scanning {document}...")
```

More examples at [my GitHub](https://github.com/Revisto/SOLID/tree/master/interface-segregation)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3w2yaxw75rruvheunhh8.jpg)


By adhering to the Interface Segregation Principle, we can craft **interfaces that are meaningful and concise, tailored specifically to the needs of clients.** This principle empowers us to create software systems that are adaptable, maintainable, and efficient. Remember, SOLID principles serve as valuable guidelines for building software that stands the test of time.


🚀 Stay tuned for more articles where we'll explore the other SOLID principles and delve into the exciting world of software engineering.

Thanks for reading. Feel free to comment your thoughts😊. Hope this post was helpful. You can hit me up on [Linked In](https://www.linkedin.com/in/revisto/) and [Github](https://www.github.com/revisto/).

Happy coding!