Hi there, I have previously asked a design question related to interfaces (Link here). I have now another question when it comes to interfaces. When have you implemented too many interfaces?
Implementing multiple interfaces with few public methods:
My current design involves multiple interfaces. An example could be:
public interface ITarget
{
void OnTargetChange();
}
public interface IDeath
{
void OnDeath();
}
public interface IVisibility
{
void OnVisibility();
}
When implmenting this, it will look something like this:
public class Player: Monobehaviour, ITarget, IDeath, IVisibility
{
public void OnTargetChange(){}
public void OnDeath(){}
public void OnVisibility(){}
}
Implementing a single interface containing multiple methods:
However, this same design can be achieved by creating a single interfaces that implements all the methods at the same time.
public interface IEntity
{
void OnTargetChange();
void OnDeath();
void OnVisibility();
}
And when implementing this
public class Player : Monobehaviour, IEntity
{
public void OnTargetChange(){}
public void OnDeath(){}
public void OnVisibility(){}
}
My question is this, what good rule of thumbs are there to follow? When is a single interface too big or too small. My personal opinion is implementing multiple interfaces with only few public methods, because thats how you achieve the highest level of loose coupled code, however the downside of this is you have to make multiple if-statements in order to determine whether a class have implemented all the different interfaces. All the if-statements can be minimized if you implement a single interface containing all the different public methods.
What is your thought on this, what rule of thumbs do you follow?