I just study the principle of separation of segregation. But after training, I got confused with the script in this example.
The concept divides interfaces into simple interfaces. This is good, but my question is hierarchy model or not?
Take the example that I studied in the book.
I have one interface for a 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 will simply simplify with a single class implementation 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 applying to other categories that do not have related properties. When you create a class for TShirt, there is no need for certification and RunningTime. Thus, in accordance with the principle of interface separability, the interface is partitioned as shown below.
Create a new interface, move the movie-related properties as shown below
public interface IMovie { int Certification { get; set; } int RunningTime { get; set; } }
So, IProduct does not have these properties and implementations, as shown 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; } }
Finally, I'm fine with that. But if it is about implementing a real method like that. When I use dependency injection, which interface I used as the type for the DVD class.
Am I embarrassed or is something missing? If I apply the inheritance logic, we can use the lower level interface, so the base interface is also inherited. But if I use it like that, how to implement it?