When you write an application that needs to read and work with two versions of data in the same way that it is best to structure classes to represent that data. I came up with three scenarios:
- General basic / specific children
- Data union
- Distinctive structures
Version 1 Car Example
byte DoorCount
int Color
byte HasMoonroof
byte HasSpoiler
float EngineSize
byte CylinderCount
Version 2 Car
byte DoorCount
int Color
enum:int MoonRoofType
enum:int TrunkAccessories
enum:int EngineType
General basic / specific children
Using this method, there is a base class of common fields between two data versions and a child class for each data version.
class Car {
byte DoorCount;
int Color;
}
class CarVersion1 : Car {
byte HasMoonroof;
byte HasSpoiler;
float EngineSize;
byte CylinderCount;
}
class CarVersion2 : Car {
int MoonRoofType;
int TrunkAccessories;
int EngineType;
}
Strong
The weak
- Existing child classes will need to change if a new version is released that removes the common field
- Data for one conceptual unit is not divided between the two definitions because of any division that is significant to itself.
Data union
Car Car .
class Car {
CarVersion version;
byte DoorCount;
int Color;
int MoonRoofType;
int TrunkAccessories;
int EngineType;
float EngineSize;
}
. , / , .
class CarVersion1 {
byte DoorCount;
int Color;
byte HasMoonroof;
byte HasSpoiler;
float EngineSize;
byte CylinderCount;
}
class CarVersion2 {
byte DoorCount;
int Color;
int MoonRoofType;
int TrunkAccessories;
int EngineType;
}
, ? , , , ?