Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing interface implementations to implement hierarchy in c#

I m writing interfaces for new project and would like to get some advice.

I have a class that have a subclass and it has a subclass. The tree of this classes is like this:

Class Car
{
    Wheels Wheel;
}
Class Wheels
{
    Rims Rim;
}

So to simplify: one car has one wheel and one wheel has one rim. (cant make up other better example, sorry).

So I would like to force this hierarchy in my interface implementation of ICar, IWheels and IRims.

So i did something like this (in C#):

ICar
{
  IWheels Wheel;
}
IWheels
{
  IRims Rim;
}

And i have a error that I can not have fields in interface implementation. So this started me thing that maybe it's wrong interface design. I would like to force interface implementations to implement this kind of hierarchy. But maybe accorting to design patterns and best practices it should be done in other way?

Could you please tell me how to design my system so that objects will be forced to implement this kind of hierarchy?

Maybe there is something not precise in my question or I missing some important info. If yes, please ask in comments.

like image 692
tomaszs Avatar asked Nov 28 '22 05:11

tomaszs


1 Answers

In your interface, you'll have to make it clear that Wheels should be a property of ICar, since you cannot declare which fields an interface implementation should have. (Fields are inner workings, so the interface should not know about it).

interface ICar
{
    IWheels Wheels
    {
       get;
    }
}
like image 148
Frederik Gheysels Avatar answered Apr 11 '23 18:04

Frederik Gheysels