0
votes

I have an interface like this

class IVersion
{
public:
   virtual char * get_version() const  = 0;
   virtual bool is_compatible(const IVersion& other) const = 0;
};

I have a set of "Device" classes, all of which need to have the above interface supported on them so that I can check for compatibility between devices. For doing so I publicly inherit each device from the above IVersion class.

Now since I am inheriting from a IVersion(and from Liskov Substitution Principle), it implies that a device is a Version. But the actual relationship that I want is that a device has a version.

I want all devices to support methods as in IVersion, but the way I have done it I think, is not correct. Can anyone suggest a better way? I can use composition, but then I will need to add methods now in IVersion, into the device base class and then internally use an IDevice implementation. Is this the best way to do what I want to achieve?

1
Then don't inherit. Instead provide a method - BЈовић
How about renaming your interface to IVersioned, thus "a device is versioned". - Iridium
To insist on Iridium's comment, is it a Version that has a version number, and can be compatible with another one or a device ? If the answer is the device, then you have just to follow Iridium's advice. - Serge Ballesta
I check for compatibility of devices based on their versions - Arun
Either device objects are versioned or have a version. You have to decide which one is more appropriate. If version is immanent part of the device that defines its properties then use inheritance and change interface name like @Iridium suggests. If version is like a label added later to the device to mark its properties (say you made a catalog of devices and created versioning system with your own semantics), then create Version class and use composition. - doc

1 Answers

1
votes

Since, there are virtual methods in your IVersion class, it seems you might need to override those ( now OR in future). So private inheritance is better alternative to composition in this case.