Original Post
I love this book, (ISBN-0-201-63361-2) I've never thought about design in terms of "encapsulating variation" until reading this. Most of my question in how to properly design a system has been answer by the concepts presented in this book. Anyway, I know I haven't mastered them as I have not had the chance to even try them in C++ (although I did some stuff with javascript using notepad :) ), but here are some of the patterns that I think are the most useful: Adapter - STL function adapters are definately useful, I can also think of many other applications. I also love the fact that it allows me more breathing room with interfaces. If the interface doesn't fit every dependant use, then an adapter can fix that. Bridge - still unsure on what this one is, but I once used a templated object to define the methods for accessing user input. That allowed me to decouple my input system from the version of Direct Input that I was relying on. To me that seems like a bridge, but that book's example is nothing like that. State - better way of implementing state machines. Seeing this leads me to believe that most uses of switch statements could redone using polymorphic objects that encapsulate the variations instead of using branching code. Factory/Abstract Factory - if I saw the code for something like this without the explanation I would've thought it was a stupid waste. However after reading I can imagine a few uses and it inspired a new idea for me that may eliminate the need for singletons. The idea which I came up with for a new design patter may be a "builder" however nothing in the book matches it. Regardless, programming a particle system for an arcade javascript game that I made, (using notepad, I needed to keep my skills up :) ) brought me to the idea of a Particle Emitter (which came from here, suggested by a member of this forum). I realized that my interpretation of a Particle Emitter was a "Generator" (well, that's what I'll call it as it seems fair enough) of objects of the type "Particle". So here is what I define as the "Generator" pattern. Generator - Intent: Separate the initialization of objects which depend on the same set of shared data, from the process of creating them. Thus allowing instances to be created without the need of forwarding all of the initializing values to every location where these objects must be created. - Motivation: Particles within a particle system are intialized with a complex set of ranges (min velocity, max velocity, min x bound, max x bound) that determine how a given particle will behave. Furthermore, particles and many other types of objects often need reference to all other objects of their type within a given collection. This requires that the objects are instantiated with those values. Passing on this data manually in every location where these objects are created can become messy. It also tightens the coupling between the implementation of the object and its creation. By using a "Generator", the method of creating an object can be changed without having to alter implementation code everywhere else within the program. Example 1 code: Particle Emitter <!--STARTSCRIPT--><!--source lang="cpp"--><div class="source"><pre> <span class="cpp-keyword">class</span> Generator() { <span class="cpp-keyword">public</span>: Generator(<span class="cpp-keyword">int</span> minx_,<span class="cpp-keyword">int</span> maxx_,<span class="cpp-keyword">int</span> miny_,<span class="cpp-keyword">int</span> maxy_,<span class="cpp-keyword">int</span> minv_,<span class="cpp-keyword">int</span> maxv_) { minx=minx_; <span class="cpp-comment">// minimum x value</span> maxx=maxx_; <span class="cpp-comment">// maximum x value</span> miny=miny_; <span class="cpp-comment">// minimum y value</span> maxy=maxy_; <span class="cpp-comment">// maximum y value</span> minv=minv_; <span class="cpp-comment">// minimum velocity value</span> maxv=maxv_; <span class="cpp-comment">// maximum velocity value</span> } <span class="cpp-keyword">int</span> minx; <span class="cpp-comment">// minimum x value</span> <span class="cpp-keyword">int</span> maxx; <span class="cpp-comment">// maximum x value</span> <span class="cpp-keyword">int</span> miny; <span class="cpp-comment">// minimum y value</span> <span class="cpp-keyword">int</span> maxy; <span class="cpp-comment">// maximum y value</span> <span class="cpp-keyword">int</span> minv; <span class="cpp-comment">// minimum velocity value</span> <span class="cpp-keyword">int</span> maxv; <span class="cpp-comment">// maximum velocity value</span> Particle &Generate() <span class="cpp-comment">// generates a new particle</span> {<span class="cpp-comment">// assume Rand(min,max) returns a random int between the values min and max</span> Particle p(Rand(minx,maxx),Rand(miny,maxy),Rand(minv,maxv),m_particles); <span class="cpp-keyword">return</span> p; } }; <span class="cpp-keyword">class</span> Particle() { <span class="cpp-keyword">public</span>: Particle(<span class="cpp-keyword">int</span> x,<span class="cpp-keyword">int</span> y,<span class="cpp-keyword">int</span> v); ... } <span class="cpp-keyword">int</span> main { Generator g(<span class="cpp-number">10</span>,<span class="cpp-number">100</span>, <span class="cpp-number">50</span>,<span class="cpp-number">500</span>, <span class="cpp-number">10</span>,<span class="cpp-number">15</span>); <span class="cpp-comment">// create new generator</span> ... <span class="cpp-comment">// to generate a new particle</span> Particle p=g.Generate(); ... } </pre></div><!--ENDSCRIPT--> Example 2 code: Texture Generator <!--STARTSCRIPT--><!--source lang="cpp"--><div class="source"><pre> <span class="cpp-keyword">class</span> Generator() { <span class="cpp-keyword">public</span>: std::map<string,texture> m_map; <span class="cpp-comment">// map of all textures by filename</span> Texture *Generate(string filename) <span class="cpp-comment">// generates a new texture</span> { Texture *t=&m_map[filename]; <span class="cpp-keyword">if</span> (!t->isLoaded()) <span class="cpp-comment">// check if texture is loaded already</span> t->Load(filename); <span class="cpp-keyword">return</span> t; } }; <span class="cpp-keyword">int</span> main { Generator g(); <span class="cpp-comment">// create new generator</span> ... <span class="cpp-comment">// to generate a new texture or reference an existing one if that is already loaded</span> Texture *t=g.Generate(<span class="cpp-literal">"test.png"</span>); ... } </pre></div><!--ENDSCRIPT--> I would implement that last example with smart pointers, but it's just an example and I don't want to make it too complex. Anyway, I cannot wait until I'm back in America doing C++ and using design patterns.