c++ - Design pattern to refactor switch statement -


I have something like the following in the header

  class messenger {public: unsigned int GetMsgType () Const {return type_; } ... Private: Enum type {MSG_DERIVED_1, MSG_DERIVED_2, ... MSG_DERIVED_N}; Unsigned int type_; ...}; Square MsgDerived1: Public MsgBase {...}; Class MsgDerived2: Public MsgBase {...}; ... class MsgDerivedN: Public MsgBase {...};  

and

 is used as ; Msgbase msgHeader; // In the input stream the peep / / is the derived message type to obtain the base class // Non-destructive input stream deserizable (msgHeader); Unsigned int msgType = msgHeader.getMsgType (); MsgDerived1 msgDerived1; MsgDerived2 msgDerived2; ... MsgDerivedN msgDerivedN; Switch (msgType) {case fills out MSG_DERIVED_1: // msgDerived1 inputstream // destructively inputStream.deserialize (msgDerived1); / * MSMDereViet1 processing * / break; Case MSG_DERIVED_2: inputStream.deserialize (msgDerived2); / * MSMDereViet1 processing * / break; ... Case MSG_DERIVED_N: InputStream Disease (msgDerivedN); / * MSMDereViet1 processing * / break; }  

It looks like the type of situation which would be quite normal and favorable to the refactoring. What would be the best way to apply the design pattern (or the new look of the basic C ++ language feature) to refact this code?

I have read that the command pattern is commonly used to refact switch statements, but it seems that only applies when choosing between algorithms to do one task. Is there a place where a factory or intangible factory pattern applies (I am not very familiar with either)? Double Send?

I have tried to give up as much unimportant references as possible, but if I miss something important then tell me and I will edit to include it. Besides, I could not find anything similar, but if it is a duplicate then I am redirected to the appropriate SOE question.

If you want to get completely fancy, then register all your derivative types with the factory with token (like 'type') that the factory wants to know what to do. Then, the factory deserialize in its table, looks at the token, and makes the right message.

  Class DerivedMessage: Public Message {Public: Static Message * Create (Stream & amp;;); Bull serialize (stream & amp;); Private: Stable BULL is registered; }; // be sure to change it to macro, use singleton, whatever you want bool DerivedMessage :: isRegistered = g_messageFactory.Register ("DerivedMessage"), DerivedMessage :: Create);  

etc. Creates a static method and assigns a new DerivedMessage and deserializes it, writes Serialize method token (in this case, hash ("DerivedMessage") ) and then serializes one of them probably to be probed It is registered that it does not bring the dead snatch by the linker.

(Specifically, this method is not required enum or other "stable list of all that may still exist". At this time I can not think of any other method for which something Circular reference to extent is not required.)


Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -