DEV Community

Muhammad Raza Bangi
Muhammad Raza Bangi

Posted on

Interface Segregation Principle (ISP) By Using PHP : SOLID Principle

Introduction:

Hey there, fellow coders!

Welcome to my article where we’re going to dive into the world of programming principles. Today’s topic? The Interface Segregation Principle (ISP). Don’t let the fancy name scare you off — I promise to make it as fun and easy to understand as playing your favorite video game!

What is the Interface Segregation Principle?

Imagine you’re in a library 📚 browsing through books. Each book covers a different topic, like science, history, or fiction. Now, what if you’re only interested in science books? You wouldn’t want to be forced to check out books from every section, right?

Well, that’s where the Interface Segregation Principle (ISP) comes into play in programming. It’s like organizing the library so that you can find exactly the books you’re interested in.

In programming, ISP says we shouldn’t make a class handle tasks it doesn’t need to. Just like in the library, we wouldn’t force everyone to check out books from every section. Instead, we’d break down big tasks into smaller ones, so each class only deals with what it’s meant for. It’s like creating a custom library card — you only borrow the books you want to read, and nothing else!

Interface segregation principle states:

A client should never be forced to implement an interface that it doesn’t use, or clients shouldn’t be forced to depend on methods they do not use.

ISP in Action: A Practical Example

Now, let’s dive into a real-life example to see how ISP works in action. We’ll look at a piece of code that’s not following the ISP and compare it to a version that does. Trust me, you’ll see a big difference!

interface EmployeeInterface {
    public function takeOrder();
    public function washDishes();
    public function cookFood();
}

class Waiter implements EmployeeInterface {
    public function takeOrder() {
      echo "take order.";  
    }

    public function washDish() {
      echo "not my job.";  
    }

    public function cookFood() {
      echo "not my job.";  
    }
}

$foo = new Waiter();
$foo->takeOrder();
Enter fullscreen mode Exit fullscreen mode

In the provided code snippet, we have an EmployeeInterface interface that defines three methods: takeOrder(), washDish(), and cookFood(). However, the Waiter class, which implements the Employee interface, is forced to implement all three methods, even though it's not responsible for washing dishes or cooking food. This violates the Interface Segregation Principle (ISP), which suggests that classes should not be forced to implement interfaces containing methods they don't need. By adhering to ISP, we can avoid bloated interfaces and ensure that each class only implements the methods relevant to its specific responsibilities. This promotes better code organization, flexibility, and maintainability in our software systems.

Now Interface Segregation Principle (ISP) comes in action:

//interfaces
interface WaiterInterface {
    public function takeOrder();
}

interface ChefInterface {
    public function cookFood();
}

interface DishWasherInterface {
    public function washDish();
}

//classes
class Waiter implements WaiterInterface {
    public function takeOrder() {
        echo "take order.";
    }
}

class Chef implements ChefInterface {
    public function cookFood() {
        echo "cook food.";
    }
}

class DishWasher implements DishWasherInterface {
    public function washDish() {
        echo "wash dish.";
    }
}
Enter fullscreen mode Exit fullscreen mode

In this updated code snippet, we’ve applied the Interface Segregation Principle (ISP) by breaking down the large Employee interface into smaller, more specialized interfaces: WaiterInterface, ChefInterface, and DishWasherInterface. Each interface contains only the methods relevant to the specific responsibilities of the corresponding class.

The Waiter class implements the WaiterInterface and provides the takeOrder() method, which is its primary responsibility. Similarly, the Chef class implements the ChefInterface with the cookFood() method, and the DishWasher class implements the DishWasherInterface with the washDish() method.

By using ISP, we ensure that each class is only concerned with the methods related to its role, promoting better code organization and maintainability. This approach allows us to easily add or modify functionality without impacting unrelated classes, making our code more flexible and adaptable to change. Overall, adhering to ISP leads to cleaner, more focused code and a more robust software design.

Conclusion:

In conclusion, the Interface Segregation Principle (ISP) provides a valuable guideline for designing interfaces in object-oriented programming. By breaking down large interfaces into smaller, specialized ones, we ensure that classes are only responsible for implementing the methods relevant to their specific roles.

In the example provided, we saw how applying ISP resulted in cleaner, more focused code. Each class, whether it be the Waiter, Chef, or DishWasher, only implemented the methods necessary for its particular responsibilities. This not only improves code readability and maintainability but also allows for easier extension and modification in the future.

By following ISP, we can create software systems that are more flexible, scalable, and resilient to change. It encourages modular design and promotes better separation of concerns, ultimately leading to more robust and efficient codebases.

Incorporating ISP into our coding practices helps us build software that is easier to understand, debug, and maintain, ultimately contributing to the overall success of our projects. So, let’s keep the principles of ISP in mind as we continue to develop software solutions, striving for code that is both elegant and effective.

Thank you for reading. :)

Top comments (0)