9

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?

1
  • The interface segregation principle is about applying interfaces to services, not to entities. In your case you are actually using the interface as a base type, since a DVD is a Product. Its impossible to apply the interface segregation principle here. Commented Aug 13, 2014 at 6:28

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

great i also think like this but the scneario creates confusion ok so inheritance is good like this scnarios

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.