Note that there are some explanatory texts on larger screens.

plurals
  1. POOverwhelmed by design patterns... where to begin?
    text
    copied!<p>I am writing a simple prototype code to demonstrate &amp; profile I/O schemes (HDF4, HDF5, HDF5 using parallel IO, NetCDF, etc.) for a physics code. Since focus is on IO, the rest of the program is very simple:</p> <pre><code>class Grid { public: floatArray x,y,z; }; class MyModel { public: MyModel(const int &amp;nip1, const int &amp;njp1, const int &amp;nkp1, const int &amp;numProcs); Grid grid; map&lt;string, floatArray&gt; plasmaVariables; }; </code></pre> <p>Where <code>floatArray</code> is a simple class that lets me define arbitrary dimensioned arrays and do mathematical operations on them (i.e. x+y is point-wise addition).</p> <p>Of course, I could use better encapsulation (write accessors/setters, etc.), but that's not the concept I'm struggling with. For the I/O routines, I am envisioning applying simple inheritance:</p> <ul> <li>Abstract I/O class defines read &amp; write functions to fill in the "myModel" object <ul> <li>HDF4 derived class</li> <li>HDF5</li> <li>HDF5 using parallel IO</li> <li>NetCDF</li> <li>etc...</li> </ul></li> </ul> <p>The code should read data in any of these formats, then write out to any of these formats. In the past, I would add an <code>AbstractIO</code> member to myModel and create/destroy this object depending on which I/O scheme I want. In this way, I could do something like:</p> <pre><code>myModelObj.ioObj-&gt;read('input.hdf') myModelObj.ioObj-&gt;write('output.hdf') </code></pre> <p>I have a bit of OOP experience but very little on the Design Patterns front, so I recently acquired the <a href="http://rads.stackoverflow.com/amzn/click/0201633612" rel="nofollow noreferrer">Gang of Four book "Design Patterns: Elements of Reusable Object-Oriented Software"</a>. OOP designers: <strong>Which pattern(s) would you recommend I use to integrate I/O with the <code>myModel</code> object?</strong> I am interested in answering this for two reasons:</p> <ul> <li>To learn more about design patterns in general</li> <li>Apply what I learn to help refactor an large old crufty/legacy physics code to be more human-readable &amp; extensible.</li> </ul> <p>I am leaning towards applying the Decerator pattern to <code>myModel</code>, so I can attach the I/O responsibilities dynamically to <code>myModel</code> (i.e. whether to use HDF4, HDF5, etc.). However, I don't feel very confident that this is the best pattern to apply. Reading the Gang of Four book cover-to-cover before I start coding feels like a good way to develop an unhealthy caffeine addiction. What patterns do you recommend?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload