DEV Community

Cover image for SOLID Design Principles: Learn the Interface-Segregation Principle
Alireza Shabani
Alireza Shabani

Posted on • Edited on

SOLID Design Principles: Learn the Interface-Segregation Principle

πŸ‘‹ 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.

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:

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")

Enter fullscreen mode Exit fullscreen mode

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.

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}...")
Enter fullscreen mode Exit fullscreen mode

More examples at my GitHub

Image description

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 and Github.

Happy coding!

Top comments (0)