jeudi 23 juin 2016

Avoid redundancy code using covariance in object oriented inheritance

I know that this problem may have been asked before, but i really do not know what to search for, as I am not even sure about my chosen title.

What I want to achieve: If I use getMeasurement(int timestep) with an Integer value not contained in the map of a Sensor object, the asked measurement should be interpolated (if possible) using a measurement specific method interpolate(...). It should get the correct two Measurements objects from the map to use for the interpolation in the super class Sensor and interpolate them, but I do not know how and if i can call the interpolation. Maybe with Generics/Typename or design patterns.

Additional Info: ASensor::measurements just contains AMeasurements. BSensor::measurements just contains BMeasurements. ... These measurements contain different value types and therefore each subclass of Measurement needs to be interpolated differently.

abstract class Sensor {
   map<int, Measurement> measurements;
   getMeasurement(int timestep);
}

class ASensor : Sensor {
   ...
}

class BSensor : Sensor {
   ...
}


abstract class Measurement {
   ...
}

class AMeasurement : Measurement {
   interpolate(AMeasurement other, int timestep);
}

class BMeasurement : Measurement {
   interpolate(BMeasurement other, int timestep);
}

If I add a method Measurement::interpolated(Measurement other, int timestep) for inheritance, this signature would not be good for the subclass, as i need to check the class type and also cast the other Measurement.

I appreciate an answer possible to code in C++11, which I am currently using.

Aucun commentaire:

Enregistrer un commentaire