I have my own INIFile class, which I wrote to read / write INI files containing fields under the header. I have several classes that I want to serialize using this class, but I'm a bit confused to do this best. I considered two possible approaches.
Method 1. Define an interface, such as ObjectPersistent, using two methods:
public interface ObjectPersistent { public void save(INIFile ini); public void load(INIFile ini); }
Each class will be responsible for using the INIFile class to output all properties to a file.
Method 2: expose all the properties of classes that require serialization through getters / setters so that savings can be processed in one centralized place:
public void savePlayer(Player p) { INIFile i = new INIFile(p.getName() + ".ini"); i.put("general", "name", p.getName()); i.put("stats", "str", p.getSTR());
The best part of method 1 is that not all properties must be open, so encapsulation is preserved. What is wrong with method 1 is that saving is not technically what the player “did”. It also links me to flat files through the ini object passed to the method, so switching to a relational database later will be a huge pain.
The best part of method 2 is that all the I / O operations are centralized in one place and the actual saving process is completely hidden from you. This can be a save to a flat file or database. What's wrong with method 2 is that I have to fully expose the internal members of the classes so that the centralized serializer can get all the data from the class.
I want it to be as simple as possible. I prefer to do it manually without using a framework. I'm also definitely not interested in using the built-in serialization provided in Java. Is something missing here? Any suggestions on which template is best suited for this, I would appreciate it. Thanks.
source share