Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>From what you've described, <strong>Decorator</strong> is not the pattern to be looking at.</p> <p>It sounds to me like you simply want to set up a chain of transformers to operate upon a common vector – i.e. a simple <em>functional composition</em>. This differs from <strong>Decorator</strong> in terms of the relationship to the object at the core - you are not building something to <em>stand in</em> for a vector, you are building something to <em>operate upon</em> it. Now when you take multiple transformers, you could theoretically imagine the second and later transformers as <em>decorators</em> of the first, but given the simplicity of the objects involved, trying to apply the GoF decorator implementation to your situation is probably going to be overkill.</p> <p>You can KISS by doing something like this:</p> <pre><code>#include &lt;vector&gt; using namespace std; typedef vector&lt;int&gt; ivec; ivec&amp; a(ivec&amp; v) { v.push_back(1); return v; } ivec&amp; b(ivec&amp; v) { v.push_back(2); v.push_back(3); v.push_back(4); return v; } ivec&amp; c(ivec&amp; v) { v.pop_back(); return v; } </code></pre> <p>There are three simple transformation functions, each written so that the output of one can be fed right into the input of the next. Then you can do something like:</p> <pre><code>ivec&amp; xform(ivec&amp; v) { return c(b(a(v))); } ivec v; xform(v); </code></pre> <p>if you want to just build up your final transform statically and apply it.</p> <p>As an alternate implementation, let's say you wanted to build up a set of transformers <em>dynamically</em>. In that case, you can push the functions into a vector of transformers and apply them one by one:</p> <pre><code>#include &lt;vector&gt; using namespace std; typedef ivec&amp; (*ivec_xformer)(ivec&amp;); typedef vector&lt;ivec_xformer&gt; xform_vec; xform_vec xforms; xforms.add(&amp;a); xforms.add(&amp;b); xforms.add(&amp;c); ivec v; for (xform_vec::iterator i = xforms.begin(); i != xforms.end(); ++i) { (*i)(v); } </code></pre> <p>This loop at the end, btw, can be further "simplified" with <code>boost::bind</code> and <code>std::for_each</code> if you're so inclined.</p> <p>The dynamic chain of transformers bears some resemblance to a <strong>Chain of Responsibility</strong>, except that there's no concept of having a particular object stop the chain by "handling" the request, i.e. there's no real <em>responsibility</em> associated with this solution – each function gets an equal crack at the vector. I'd suggest this pattern needs a better name – one probably already exists out there, as this kind of functional composition in OO programming is not uncommon, but it eludes me at the moment.</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