Is there a pattern that is good to use when saving and loading different file formats?
For example, I have a complicated class hierarchy for the document, but I want to support a few different file formats.
I thought about the Strategy pattern, but I'm not convinced because of the need to access every part of the object in order to save and load it.
You could use a Visitor Pattern, it allows to iterate over your hierachy doing different operations depending of the node the Visitor is currently processing.
Bad news: you probably need to add at least a virtual method at the top of the hierarchy, and maybe redefine it in some derived classes, and the visitor still access the data of the nodes, but you decouple the file format, as different visitors implementations can write the data gathered in different ways.
Take a look also at the memento pattern if hiding the class hierachy data is a must. This article could also be helpful.
Edit: Link to the original Memento pattern article using google cache
You might want to take a look at the Builder pattern. GoF page 97..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With