Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could convert classes that create messages (MessageOne, MessageTwo ...) into message factories and register them with top level MessageFactory on initialization. </p> <p>Message factory could hold map of MessageX::Header -> instance of MessageXFactory kind of map.</p> <p>In CreateMessage you would find instance of MessageXFactory based on message header, retrieve the reference to MessageXFactory and then call it's method that would return instance of the actual MessageX.</p> <p>With new messages you no longer have to modify the 'switch', you just need to add an instance of new MessageXFactory to the TopMessageFactory.</p> <p>example:</p> <pre><code>#include &lt;iostream&gt; #include &lt;map&gt; #include &lt;string&gt; using namespace std; struct Message { static const int id = 99; virtual ~Message() {} virtual int msgId() { return id; } }; struct NullMessage : public Message { static const int id = 0; virtual int msgId() { return id; } }; struct MessageOne : public Message { static const int id = 1; virtual int msgId() { return id; } }; struct MessageTwo : public Message { static const int id = 2; virtual int msgId() { return id; } }; struct MessageThree : public Message { static const int id = 3; virtual int msgId() { return id; } }; struct IMessageFactory { virtual ~IMessageFactory() {} virtual Message * createMessage() = 0; }; struct MessageOneFactory : public IMessageFactory { MessageOne * createMessage() { return new MessageOne(); } }; struct MessageTwoFactory : public IMessageFactory { MessageTwo * createMessage() { return new MessageTwo(); } }; struct TopMessageFactory { Message * createMessage(const string&amp; data) { map&lt;string, IMessageFactory*&gt;::iterator it = msgFactories.find(data); if (it == msgFactories.end()) return new NullMessage(); return (*it).second-&gt;createMessage(); } bool registerFactory(const string&amp; msgId, IMessageFactory * factory) { if (!factory) return false; msgFactories[msgId] = factory; return true; } map&lt;string, IMessageFactory*&gt; msgFactories; }; int main() { TopMessageFactory factory; MessageOneFactory * mof = new MessageOneFactory(); MessageTwoFactory * mtf = new MessageTwoFactory(); factory.registerFactory("one", mof); factory.registerFactory("two", mtf); Message * msg = factory.createMessage("two"); cout &lt;&lt; msg-&gt;msgId() &lt;&lt; endl; msg = factory.createMessage("one"); cout &lt;&lt; msg-&gt;msgId() &lt;&lt; endl; } </code></pre>
    singulars
    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.
    1. VO
      singulars
      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