I am just learning Interface Segregation Principle. But after learning i am confused with the scenario in the example.
The concept is separating the interfaces into simple interfaces. That is well but my question is with hierarchy model or not?
Take the example i studied in the book.
I have one Interface for product with the following properties
public interface IProduct
{
decimal Price { get; set; }
decimal WeightInKg { get; set; }
int Stock { get; set; }
int Certification { get; set; }
int RunningTime { get; set; }
}
I just simplify with one class implmentation from the interface
public class DVD : IProduct
{
public decimal Price { get; set; }
public decimal WeightInKg { get; set; }
public int Stock { get; set; }
public int Certification { get; set; }
public int RunningTime { get; set; }
}
The problem is when apply to the other categories which have not related properties. When create a class for TShirt, there is no needed for Certification and RunningTime. So as per the Interface Segregation Principle, the interface is separated like below
Create a new interface, move the movie related properties to this one like below
public interface IMovie
{
int Certification { get; set; }
int RunningTime { get; set; }
}
So the IProduct does not have these properties and the implementation like below
public class TShirt : IProduct
{
public decimal Price { get; set; }
public decimal WeightInKg { get; set; }
public int Stock { get; set; }
}
public class DVD : IProduct, IMovie
{
public decimal Price { get; set; }
public decimal WeightInKg { get; set; }
public int Stock { get; set; }
public int Certification { get; set; }
public int RunningTime { get; set; }
}
Conceptionally i am ok with this. But if this about real method implementation like this. When i use dependency injection which interface i used as the type for DVD class.
I am confused or i missing something ? If i apply the inheritance logic we can use the lower level interface so base interface also inherited. But if i use like this how can implement?
DVDis aProduct. Its impossible to apply the interface segregation principle here.