tl;dr: Is there a design pattern to load an XML file and construct hierarchical polymorphous objects based on the contents?
Say (example) I have a derived class Container
derived from Storage
, with a list of items.
In some way I have to load an XML file, this file has:
- Some generic info for Storage (name, location).
- That I need a Container implementation (eg: not a Cabinet or Shelves).
- Items with information about them.
Currently, I open an QXmlStreamReader in main, see what Storage I need, construct one, pass the reader to Storage to be loaded. Storage in it's turn iterates all items, creates them and passes the reader on for the Item contents. This works, without polymorphism. (lots of switches, ugh)
The problem is that when I want to make things cleanly polymorphous, I reach a problem since I both have to initialize the base, and the derived.
Which needs the Call Super pattern, and it fails when I change the order of the XML file, since QXmlStreamReader is stateful. (has no reset)
Which smells, both because of the call super and the fact that all classes know about the xml file.
The problem:
class Storage {
list Items;
string location;
virtual getItem();
virtual loadXML(reader);
}
class Container : Storage {
getItem();
loadXML(reader); // needs call super
}
class Item {
string name;
int size;
virtual loadXML(reader);
}
class Thing : Item {
specific property;
loadXML(reader); // needs call super
}
What other design pattern can I use to construct such hierarchical implementation based on a XML file?
Aucun commentaire:
Enregistrer un commentaire