

We are reducing our class implementation only to required actions without any additional or unnecessary code. Therefore, we make sure that our class needs all these actions to complete its task.Īnother benefit is that the Interface Segregation Principle increases the readability and maintainability of our code. But in real-world projects, we often come up with an interface with multiple methods, which is perfectly normal as long as those methods are highly related to each other. Of course, due to the simplicity of our example, we can make a single interface with a single method inside it. We can see from the example above, that smaller interface is a lot easier to implement due to not having to implement methods that our class doesn’t need. What are the Benefits of the Interface Segregation Principle Public MultiFunctionalCar(ICar car, IAirplane airplane) Or if we already have implemented the Car class and the Airplane class, we can use them inside our class by using the decorator pattern: public class MultiFunctionalCar : IMultiFunctionalVehicle The first one is to implement the required methods: public class MultiFunctionalCar : IMultiFunctionalVehicle Once we have our higher level interface, we can implement it in different ways. Learning Web API? Get our eBook ASP.NET Core Web API Best Practices and become an expert for FREE! > GET THE BOOK << public interface IMultiFunctionalVehicle : ICar, IAirplane We can even use a higher level interface if we want in a situation where a single class implements more than one interface: Public class MultiFunctionalCar : ICar, IAirplaneĬonsole.WriteLine("Drive a multifunctional car") Ĭonsole.WriteLine("Fly a multifunctional car") The first thing we are going to do is to divide our IVehicle interface: public interface ICarĪs a result, our classes can implement only the methods they need: public class Car : ICar Implementing the ISP In the Current Solution
#Principle of segregation code#
So, in order to fix this problem, we are going to do some refactoring to our code and write it in accordance to ISP. Furthermore, we would have to put an additional effort to document our class so that users know why they shouldn’t be using the not implemented method. That is a bad idea because we should be writing our code to do something and not just to throw exceptions. The other method, which is not required, is implemented to throw an exception. It contains only one required declaration per each class. Now we can see what the problem with the IVehicle interface is.
#Principle of segregation download#
To download the source code for this project, check out the Interface Segregation Principle Project Source Code.

But now, as a consequence, we don’t need all the methods from that interface, just some of them. Soon after, the customer decides that they want another feature that is similar to the previous one and we decide to implement the same interface in another class. We start with some code and from that code, an interface emerges with the required declarations. Let’s imagine that we are starting a new feature on our project. So, this is the basic definition which we can read in many different articles, but what does this really mean? The Interface Segregation Principle states that no client should be forced to depend on methods it does not use.
