<p>Does anybody recommend a design pattern for taking a binary data file, parsing parts of it into objects and storing the resultant data into a database?  </p>

<p>I think a similar pattern could be used for taking an XML or tab-delimited file and parse it into their representative objects.</p>

<p>A common data structure would include:</p>

<blockquote>
  <p>(Header) (DataElement1) (DataElement1SubData1) (DataElement1SubData2)(DataElement2) (DataElement2SubData1) (DataElement2SubData2) (EOF)</p>
</blockquote>

<p>I think a good design would include a way to change out the parsing definition based on the file type or some defined metadata included in the header.  So a <a href="http://www.oodesign.com/factory-method-pattern.html" rel="nofollow noreferrer">Factory Pattern</a> would be part of the overall design for the Parser part.</p>

[{'answer_id': 11681, 'author': 'Campbell', 'author_id': 942, 'author_profile': 'https://Stackoverflow.com/users/942', 'pm_score': 1, 'selected': False, 'text': '<p>The Strategy pattern is maybe one you want to look at. The strategy being the file parsing algorithm.</p>\n\n<p>Then you want a separate strategy for database insertion.</p>\n'}, {'answer_id': 11712, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 5, 'selected': True, 'text': "<ol>\n<li>Just write your file parser, using whatever techniques come to mind</li>\n<li>Write lots of unit tests for it to make sure all your edge cases are covered</li>\n</ol>\n\n<p>Once you've done this, you will actually have a reasonable idea of the problem/solution.</p>\n\n<p>Right now you just have theories floating around in your head, most of which will turn out to be misguided.</p>\n\n<p>Step 3: Refactor mercilessly. Your aim should be to delete about half of your code</p>\n\n<p>You'll find that your code at the end will either resemble an existing design pattern, or you'll have created a new one. You'll then be qualified to answer this question :-)</p>\n"}, {'answer_id': 25129, 'author': 'Henrik Gustafsson', 'author_id': 2010, 'author_profile': 'https://Stackoverflow.com/users/2010', 'pm_score': 2, 'selected': False, 'text': '<p>I fully agree with Orion Edwards, and it is usually the way I approach the problem; but lately I\'ve been starting to see some patterns(!) to the madness.</p>\n\n<p>For more complex tasks I usually use something like an <a href="http://en.wikipedia.org/wiki/Interpreter_pattern" rel="nofollow noreferrer">interpreter</a> (or a <a href="http://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow noreferrer">strategy</a>) that uses some <a href="http://en.wikipedia.org/wiki/Builder_pattern" rel="nofollow noreferrer">builder</a> (or <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="nofollow noreferrer">factory</a>) to create each part of the data.</p>\n\n<p>For streaming data, the entire parser would look something like an <a href="http://en.wikipedia.org/wiki/Adapter_pattern" rel="nofollow noreferrer">adapter</a>, adapting from a stream object to an object stream (which usually is just a queue).</p>\n\n<p>For your example there would probably be one builder for the complete data structure (from head to EOF) which internally uses builders for the internal data elements (fed by the interpreter). Once the EOF is encountered an object would be emitted.</p>\n\n<p>However, objects created in a switch statement in some factory function is probably the simplest way for many lesser tasks. Also, I like keeping my data-objects immutable as you never know when someone shoves concurrency down your throat :)</p>\n'}, {'answer_id': 137748, 'author': 'Peter Wone', 'author_id': 1715673, 'author_profile': 'https://Stackoverflow.com/users/1715673', 'pm_score': 1, 'selected': False, 'text': '<p>Use <a href="http://en.wikipedia.org/wiki/Lex_programming_tool" rel="nofollow noreferrer">Lex</a> and YACC. Unless you devote the next ten years exclusively to this subject, they will produce better and faster code every time.</p>\n'}]
['https://Stackoverflow.com/questions/11680', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1048/']