加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 03:42:14 所属栏目:百科 来源:网络整理
导读:我在标题中有类似的内容 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 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-destructively
inputStream.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 like
bool DerivedMessage::isRegistered =
      g_messageFactory.Register(Hash("DerivedMessage"),DerivedMessage::Create);

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读