I have a set information that I am able to read in as raw data which corresponds to different physical quantities depending on which device provided that information. Every device contains the same amount of data and the same types, just different fields are interpreted differently. Here's how I have implemented it so far:
public abstract class SuperData {
int voltage1;
int voltage2;
SuperData(int v1, int v2) {
voltage1 = v1;
voltage2 = v2;
}
public int getVoltage1 { return voltage1; }
public int getVoltage2 { return voltage2; }
}
public class CarAData extends SuperData {
CarAData(int v1, int v2) { super(v1, v2); }
public int getKeyVoltage { return getVoltage1(); }
public int getLightVoltage { return getVoltage2(); }
}
public class CarBData extends SuperData {
CarAData(int v1, int v2) { super(v1, v2); }
public int getStarterVoltage { return getVoltage1(); }
public int getCigarretteVoltage { return getVoltage2(); }
}
This is just a trivial example; in reality it is about 60 fields that are just a byte array coming over Bluetooth. I am using a factory to create the correct object based on another data stream but the problem is it requires the use of instanceof
down the road to know which method to call.
So what pattern is the best way to represent data which has different meanings?