This situation happened to me many times and I have no idea how to solve it.
Interface segregation principle was made to prevent situations, when some interface implementations don't use it's functionality - that's obvious. There is often situation that I have a list of interfaces and I want to do something with them. Let's look at example without ISP:
public interface IPerson
{
void Run();
void Eat();
}
public class AbcPerson : IPerson
{
void Run(){};
void Eat(){};
}
public class XyzPerson : IPerson
{
void Run(){};
void Eat(){};
}
List<IPerson> People {get;set;}
And I want to run with every person.
foreach(var person in People)
{
person.Run();
}
Now I want to eat with all of them.
foreach(var person in People)
{
person.Eat();
}
Now if I would like to use ISP I should change code to:
public interface IRunnable
{
void Run();
}
public interface IEatable
{
void Eat();
}
public class AbcPerson : IRunnable,IEatable
{
void Run(){};
void Eat(){};
}
public class XyzPerson : IRunnable,IEatable
{
void Run(){};
void Eat(){};
}
How should I make my list of people? Should I make two lists of Runable and Eatable and add objects(ugly) or maybe second approach- create one list and cast them if it's possible(ugly)? I have no idea what is the best convention to do that.
This example maybe is not the best example I could imagine but I hope you know what I mean.
EDITED: I changed interfaces and classes and principle name.