Reputation: 2030
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?
Upvotes: 9
Views: 5472
Reputation: 66499
If you know that anything that's a movie is always going to also be a product, you could define your interfaces like this, where IMovie
extends IProduct
:
public interface IProduct
{
decimal Price { get; set; }
decimal WeightInKg { get; set; }
int Stock { get; set; }
}
public interface IMovie : IProduct
{
int Certification { get; set; }
int RunningTime { get; set; }
}
Then your DVD
class just implements the IMovie
interface:
public class DVD : 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; }
}
Using your other example, perhaps your TShirt
implements an IClothing
interface, which is also a product:
public class IClothing : IProduct
{
int Size { get; set; }
Color Color { get; set; }
}
public class TShirt : IClothing
{
public decimal Price { get; set; }
public decimal WeightInKg { get; set; }
public int Stock { get; set; }
public int Size { get; set; }
public Color Color { get; set; }
}
Now when you inject your dependencies, you can ask for an instance of IMovie
or IClothing
.
Upvotes: 10