Decoding SOLID Principles (#4 Interface Segregation)

Shubhi Puntambekar
2 min readMay 11, 2023

This a continuing series of the SOLID Principles. If you have not gone through the previous thread, I request you to check this post before continuing to read this one.

Interface Segregation Principle

The interface segregation principle states that “Many client-specific interfaces are better than a general purpose interface.” This simply means that larger interfaces can be split into smaller interfaces and the clients do not have to implement irrelevant functions that they do not need in their classes.

This is indeed a very easy-to-understand principle. Let us understand this with the help of an example.

class RestaurantEmployee:
def decide_menu():
pass
def cook_food():
pass
def take_order():
pass
def serve_food():
pass

class Waiter(RestaurantEmployee):
def decide_menu():
# do something
def cook_food():
# do something
def take_order():
# do something
def serve_food():
# do something

class Chef(RestaurantEmployee):
def decide_menu():
# do something
def cook_food():
# do something
def take_order():
# do something
def serve_food():
# do something

Now if you see that the Waiter is also a RestaurantEmployee and the Chef is also a RestaurantEmployee. But the functions which they are performing are irrelevant to their jobs. For example, the Waiter doesn’t decide_menu() and cook_food(). Similarly, the Chef also doesn’t take_order() and serve_food(). What we can do in this case is create smaller interfaces, so that the respective employee does the function which is relevant to their job.

class WaiterInterface:
def take_order():
pass
def serve_order():
pass

class ChefInterface:
def decide_menu():
pass
def cook_food():
pass

class Waiter(WaiterInterface):
def take_order():
print("Taking order..")
def serve_order():
print("Serving order..")

class Chef(ChefInterface):
def decide_menu():
print("Deciding menu")
def cook_food():
print("Cooking food")

Now with the help of segregating these interfaces, the subclasses perform the functions which are related to their requirements. This can also be useful if we need to add different types of waiters or chefs, so the methods will be the same but the functionality can change in their respective implementations. And with this, we have successfully implemented the Interface Segregation Principle.

To continue reading the last topic in this thread, click here.

--

--