Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better design pattern/method to use?

I've currently completed one of two phases of a project that required I write database information to XML using C++. While use of a third party tool was used to do the actually formatting of XML tags and data, I still had to design a model along with business logic to take the database tables and map them into XML structures.

For this I ended up creating an individual class for each XML structure, resulting in a large amount of classes (~75). Each class had the knowledge of how to read its associated table and serialize itself to XML through the third party tool. In the end the system worked very well ( on time and budget ) and output errors were extremely easy to find.

Phase two is almost identical however instead of formatted text it will be binary data. So while I am still considering utilizing the same strategy used in phase one, I would like to inquire, is a better method or design pattern that would lend itself to this problem? Particularly, due to the large amount of dependancies in some of the XML classes in phase one, unit testing was very difficult.

like image 391
Matthew Avatar asked Dec 29 '22 05:12

Matthew


1 Answers

You are describing a classic application of the Visitor pattern. You need for two purposes to traverse your object model, one time outputting XML, the other time binary data. It's well explained in the gang of four's book.

Each element of your model has to accept a visitor of a recognised type (typically IVisitor), and it then calls a method called, typically, AcceptVisitor on this visitor. This is the method that translates the object into XML, binary data, printable format or whatever. It may also then direct the visitor to child objects and so on. Then you write an XmlVisitor that implements IVisitor, and "visit" your structure with it - the result is XML. Similary, you can "visit" with a BinaryVisitor and get your binary output.

like image 147
David M Avatar answered Dec 31 '22 19:12

David M