c – 重构switch语句的设计模式

c – 重构switch语句的设计模式,第1张

概述我在标题中有类似的内容 class MsgBase{ public: unsigned int getMsgType() const { return type_; } ... private: enum Types { MSG_DERIVED_1, MSG_DERIVED_2, ... MSG_DERIVED_N }; unsigned int type 我在标题中有类似的内容
class MsgBase{  public:    unsigned int getMsgType() const { return type_; }    ...  private:    enum Types { MSG_DERIVED_1,MSG_DERIVED_2,... MSG_DERIVED_N };    unsigned int type_;    ...};class MsgDerived1 : public MsgBase { ... };class MsgDerived2 : public MsgBase { ... };...class MsgDerivedn : public MsgBase { ... };

并用作

MsgBase msgheader;// peeks into the input stream to grab the// base class that has the derived message type// non-destructivelyinputStream.deserializePeek( msgheader ); unsigned int msgType = msgheader.getMsgType();MsgDerived1 msgDerived1;MsgDerived2 msgDerived2;...MsgDerivedn msgDerivedn;switch( msgType ){  case MSG_DERIVED_1:    // fills out msgDerived1 from the inputStream    // destructively    inputStream.deserialize( msgDerived1 );    /* do MsgDerived1 processing */    break;  case MSG_DERIVED_2:    inputStream.deserialize( msgDerived2 );    /* do MsgDerived1 processing */    break;  ...  case MSG_DERIVED_N:    inputStream.deserialize( msgDerivedn );    /* do MsgDerived1 processing */    break;}

这似乎是一种相当普遍并且非常适合重构的情况.应用设计模式(或基本C语言功能重新设计)来重构此代码的最佳方法是什么?

我已经读过Command模式通常用于重构switch语句,但这似乎只适用于在执行任务的算法之间进行选择.这是工厂或抽象工厂模式适用的地方(我不是很熟悉)?双重派遣?

我试图忽略尽可能多的无关紧要的背景,但如果我错过了重要的事情,请告诉我,我会编辑包含它.此外,我找不到任何类似的东西,但如果这是重复只是重定向到适当的SO问题.

解决方法 拉类型和输出MsgBase,它们不属于那里.

如果您想要完全看中,请将所有派生类型与工厂一起注册,以及工厂用来知道要制作什么的令牌(例如“类型”).然后,工厂在其表中反序列化时查找该标记,并创建正确的消息.

class DerivedMessage : public Message{public:   static Message* Create(Stream&);   bool Serialize(Stream&);private:   static bool isRegistered;};// sure,turn this into a macro,use a singleton,whatever you likebool DerivedMessage::isRegistered =      g_messageFactory.Register(Hash("DerivedMessage"),DerivedMessage::Create);

Create static方法分配一个新的DerivedMessage并对其进行反序列化,Serialize方法写入令牌(在本例中为Hash(“DerivedMessage”)),然后自行序列化.其中一个应该测试isRegistered,以便它不会被链接器剥离.

(值得注意的是,这种方法不需要枚举或其他“可以存在的所有东西的静态列表”.此时我想不出另一种在某种程度上不需要循环引用的方法.)

总结

以上是内存溢出为你收集整理的c – 重构switch语句的设计模式全部内容,希望文章能够帮你解决c – 重构switch语句的设计模式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1257873.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存