Recently, I came across a large interface with many methods. I wonder why a
class need to implement these methods which are not related to it.
Fortunately, while searching for a solution, I discovered the Interface
Segregation Principle. It is one of five SOLID principles in object-oriented design.
This blog post reviews the Interface Segregation Principle (ISP) and
shows how it can be applied using Java examples.
Interface Segregation Principle
It tells us:
Clients should not be force to depend upon interfaces that they do not use.
It means we shouldn't make our interfaces bigger. We should not pollute our
interfaces with irrelevant methods.
And force the classes to implement methods they don't care about. Make
smaller, more specific interfaces instead of one big one that does everything.
To help you understand this better, let's look at the busy world of a
restaurant.
Bad Design: Violates ISP
Consider the situation: we are creating software to control how a restaurant
works. Perhaps our first thought is to design a single interface for every
employee:
Now, let's consider our staff roles: a Chef, a Waiter.
The Chef class implements the IRestaurant interface.
Waiter class also implements the IRestaurant interface.
As we can see, both the Chef and Waiter classes implement the
IRestaurant interface. But we are forcing the Chef to take on
all of the restaurant's responsibilities. The Chef class implements
methods such as takeOrder() and washDishes(), which are not part
of their job description. This is also true of the Waiter. The
Waiter class is also required to include methods such as
cookDish() and washDishes(). Here we end up with
UnsupportedOperationException or empty method bodies.
It is making our code:
- Bloated: In our Chef and Waiter class, there are some unnecessary methods.
- Confusing: It is not immediately clear what a Chef or Waiter does.
- Fragile: Any change to the IRestaurant interface would cause the Chef and Waiter classes to change, even if it was irrelevant to them.
Small, Specific Interfaces: Now we are on the right path
So, according to the ISP,
each interface should be responsible for a specific set of tasks. We
must create multiple, smaller interfaces with related responsibilities.
Let's refactor the IRestaurant interface. We built two interfaces,
ICook and IWaiter.
So Chef implements the ICook interface.
And Waiter implements the IWaiter interface.
The Benefits of ISP
- Clarity and Readability: Each class now only includes methods that are directly relevant to its role.
- Reduced Coupling: Our classes are no longer reliant on methods they don't employ. Changes to one interface will not cause unrelated classes to change.
- Increased Flexibility: If we add a new role, the existing interfaces will not be affected.
The Interface Segregation Principle is an effective tool for creating robust
and maintainable Java applications. With these monolithic interfaces, we can
create Java applications that are more flexible and easier to understand.
Happy coding!!! 😊
No comments:
Post a Comment