I'm facing a similar scenario to this issue and I was wondering if it's possible to get around a cleaner solution.
Supposing we have a party that has People on it, We have the interface:
public interface IPerson {
void Arrive();
void Dance();
}
And its implementations, people that are able to dance
public class PersonThatCanDance : IPerson {
public void Arrive() {
//Logic related to arriving
}
public void Dance() {
//Logic related to dancing
}
}
and those who aren't
public class PersonThatCannotDance : IPerson {
public void Arrive() {
//Logic related to arriving
}
public void Dance() {
}
}
Knowing this, now we have the party with 2 people
public class Party {
private IPerson person1;
private IPerson person2
private void StartParty() {
person1.Arrive();
person2.Arrive();
}
private void StartMusic() {
person1.Dance();
person2.Dance();
}
}
With this scenario settled, the problem comes when we want to make the party not care if the person is a person that can dance or not. The party throws the people into dancing no matter if they have functionality to do so.
From what I understand, the existence of the Dance() method in PersonThatCannotDance is a violation of ISP but making the party not care about what kind of person is preventing me from respecting the principle.
Is there a way to get around this? Thank you.
yes you are right. it is violation of ISP.
Now Implementations looks like this, people that are able to dance
and those who aren't
In Party Class
Now I hope this solution will work. please let me know if still have doubt.