To assign uni, I write a java program that must save and load the hierarchy of objects into XML files. For example, a program creates a Zoo class with a list of subclasses of "Animals", saves it in XML, and loads it again the next time the program starts.
The file may look like
<Zoo> <Animals> <Lion> <name>Leo</name> <age>5</age> <roar>fearsome</roar> </Lion> <Elephant> <name>Dumbo</name> <age>10</age> <ears>let him fly</ears> </Elephant> </Animals> </Zoo>
There are a small finite number of subclasses of Animal I that need to be supported (~ 5). Each subclass of an animal has individual attributes (e.g., Ears and Roar). I am confused as to what is the best design template for creating an object and creating a file.
I currently have an XMLCreator class with methods like void createZooElement(Zoo) , void createLionElement(Lion) , etc. etc., and the XMLReader class with private Zoo createZoo(File) , private Lion createLionObject(Element) .
Is this a good way to go if it was the code you expect from others in the future? Or should each object have a constructor method that takes a file / element as a parameter and another method that returns a / XMLElement file? What is the path with maximum encapsulation / maintainability?
source share