Note that there are some explanatory texts on larger screens.

plurals
  1. POType mapping in object factory
    primarykey
    data
    text
    <p>Suppose that I have two sets of associated types, for example <code>Animal</code>s and their <code>Offspring</code>:</p> <pre><code>/* Animal types */ struct Animal { virtual string getType() const = 0; }; struct Cat : public Animal { virtual string getType() const { return "Cat"; } }; struct Dog : public Animal { virtual string getType() const { return "Dog"; } }; /* Offspring types */ struct Offspring { virtual string getType() const = 0; }; struct Kitten : public Offspring { virtual string getType() const { return "Kitten"; } }; struct Puppy : public Offspring { virtual string getType() const { return "Puppy"; } }; </code></pre> <p>I am trying to implement a factory which, given an <code>Animal</code> will return an object of the associated <code>Offspring</code> type (e.g. if the <code>Animal</code> is in fact a <code>Dog</code>, the factory will return a <code>Puppy</code>).</p> <p>My first attempt at implementing such a factory looks like this:</p> <pre><code>// First attempt at OffspringFactory class OffspringFactory1 { static Offspring* createKitten() { return new Kitten(); } static Offspring* createPuppy() { return new Puppy(); } public: // Create an Offspring according to the Animal type static Offspring* getOffspring(const Animal&amp; a) { // Static mapping of Animal types to Offspring factory functions static map&lt;string, Offspring* (*)()&gt; factoryMap; if (factoryMap.empty()) { factoryMap["Dog"] = &amp;createPuppy; factoryMap["Cat"] = &amp;createKitten; } // Lookup our Offspring factory function map&lt;string, Offspring* (*)()&gt;::const_iterator fnIt = factoryMap.find(a.getType()); if (fnIt != factoryMap.end()) return fnIt-&gt;second(); else throw "Bad animal type"; } }; </code></pre> <p>It works fine, but I've resorted to a string-based mapping rather than something purely type-based. In trying to move towards a more type-based implementation I arrived at this:</p> <pre><code>// Second attempt at OffspringFactory class OffspringFactory2 { // Mapping Animal types to Offspring types template&lt;typename TAnimal&gt; struct OffspringMapper; template&lt;&gt; struct OffspringMapper&lt;Cat&gt; { typedef Kitten offspring_type; }; template&lt;&gt; struct OffspringMapper&lt;Dog&gt; { typedef Puppy offspring_type; }; // Factory method template&lt;typename TAnimal&gt; static Offspring* create() { return new OffspringMapper&lt;TAnimal&gt;::offspring_type(); } public: // Create an Offspring according to the Animal type static Offspring* getOffspring(const Animal&amp; a) { // Static mapping of Animal type strings to Offspring factory functions static map&lt;string, Offspring* (*)()&gt; factoryMap; if (factoryMap.empty()) { factoryMap["Dog"] = &amp;create&lt;Dog&gt;; factoryMap["Cat"] = &amp;create&lt;Cat&gt;; } // Lookup our Offspring factory function map&lt;string, Offspring* (*)()&gt;::const_iterator fnIt = factoryMap.find(a.getType()); if (fnIt != factoryMap.end()) return fnIt-&gt;second(); else throw "Bad animal type"; } }; </code></pre> <p>Frankly, I'm not sure I've improved anything here: I still have my string mapping, along with quite a few more lines of less readable code... </p> <p>Is there any merit in the second implementation over the first, and is there any way I can get rid of that map?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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