<p>Is there a pattern that is good to use when saving and loading different file formats?</p>

<p>For example, I have a complicated class hierarchy for the document, but I want to support a few different file formats.</p>

<p>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.</p>

[{'answer_id': 81322, 'author': 'erlando', 'author_id': 4192, 'author_profile': 'https://Stackoverflow.com/users/4192', 'pm_score': 1, 'selected': False, 'text': '<p>You might want to take a look at the Builder pattern. GoF page 97..</p>\n'}, {'answer_id': 81345, 'author': 'Patrick Huizinga', 'author_id': 15200, 'author_profile': 'https://Stackoverflow.com/users/15200', 'pm_score': 0, 'selected': False, 'text': '<p>How about (something based on) the <a href="http://en.wikipedia.org/wiki/Template_method_pattern" rel="nofollow noreferrer">Template method pattern</a>?</p>\n\n<p>One superclass knows how to rip apart the class hierarchy, but relies on its subclasses to actually do something useful with it.</p>\n'}, {'answer_id': 81397, 'author': 'Ricardo Amores', 'author_id': 10136, 'author_profile': 'https://Stackoverflow.com/users/10136', 'pm_score': 3, 'selected': True, 'text': '<p>You could use a <a href="http://en.wikipedia.org/wiki/Visitor_pattern" rel="nofollow noreferrer">Visitor Pattern</a>, it allows to iterate over your hierachy doing different operations depending of the node the Visitor is currently processing.</p>\n\n<p>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.</p>\n\n<p>Take a look also at the <a href="http://en.wikipedia.org/wiki/Memento_pattern" rel="nofollow noreferrer">memento pattern</a> if hiding the class hierachy data is a must. <a href="http://www.ondotnet.com/pub/a/dotnet/2002/09/09/memento.html" rel="nofollow noreferrer">This article</a> could also be helpful.</p>\n\n<p>Edit: Link to the original <a href="http://webcache.googleusercontent.com/search?q=cache:www.ondotnet.com/pub/a/dotnet/2002/09/09/memento.html" rel="nofollow noreferrer">Memento pattern article</a> using google cache </p>\n'}]
['https://Stackoverflow.com/questions/81288', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4002/']