SQLite and Java Beginner here,
I'm currently working on a game where the player obtains random items, since I plan on including a huge amount of items, I have stored the item information in a SQLite db.
The thing is, my item objects have their own behavior objects in addition to fields like String name, int power, etc. This makes converting results to objects a more complicated endeavor. Currently I am referring to these behaviors as Strings in the db, and then instantiating the correct behavior object using a Switch statement at Item Creation. This is just a semi-fictional example to show what I mean:
Item item = new Item();
String name = resultSet.getString("Name");
String activationEffect = resultSet.getString("Activation Effect");
item.setName(name);
switch (activationEffect){
case "Smack Fools":
item.setActivationEffect(new SmackFools());
break;
case "Have a Nap":
item.setActivationEffect(new NapTime());
break;
default:
break;
Alternatively I could make item.setActivationEffect take a String and do the switch statement itself, but that doesn't really change anything. Is there a better way to accomplish this?
Serializableand then store the entire serialized object graph in a database column. You can do something similar by "stringifying" your object into a JSONStringand then saving the String to a database field. Both the serialized form and a JSONStringcan be deserialized into the original object. Care must be exercised so that your serialized forms are efficient and correct.