What is the preferred way to store different versions of data?

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

  • OOP parameter

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;     //boolean if Version 1
    int TrunkAccessories; //boolean if Version 1
    int EngineType;       //CylinderCount if Version 1
    float EngineSize;     //Not used if Version2
}

  • ... .

  • .
  • , .
  • . .

. , / , .

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;
}

  • , ​​ .

  • -.

, ? , , , ?

+5
3

, , -?

/ - . . . , , . , .

, , . , , , , , / . Voila, "" .

, . "" , .

: - "". , - , "" . " ", "", "OptionContainer" .. , YMMV.

+1

. , "" , ! , , ;)

+1

:

,

, , , , 1, 2 n .

- , "" . , MoonRoofType, HasMoonRoof, . , , , .

, , .

, . , Save Load / , .

+1

All Articles