标签:tdm 过程 efault str case int ESS 带来 make
switch语句改为多态结构更好些。
1. 常规switch
#include
enum EnumType
{
enumOne,
enumTwo,
enumThree
};
void showMessage(int type)
{
switch (type)
{
case enumOne:
printf("This is message one\n");
break;
case enumTwo:
printf("This is message two\n");
break;
case enumThree:
printf("This is message three\n");
break;
default:
printf("This is wrong message\n");
break;
}
}
int main()
{
//常规switch
showMessage(enumOne);
showMessage(enumTwo);
showMessage(enumThree);
return 0;
}
2.多态+std::map取代switch
#include
上述完全是一个面向过程到面向对象的转变:将每个case分支都作为一个子对象,然后用C++语言的多态性去动态绑定。这样做确实是带来了性能上的损失,但是在当今的CPU计算能力而言,这是可以忽略的,而它带来的好处却很有用:
(1)分支的增减只要继续派生即可;
(2)子类代表了一个case,比必须用type去硬编码的case语句更加具有可读性;
(3)代码的可读性增强,使得分支的维护性增加;
(4)面向对象的思想更加符合人看世界的方式;
(5)避免了漏写break语句造成的隐蔽错误。
3.函数指针+std::map取代switch
#include
4.状态模式取代switch
#include
class Context;
class State
{
public:
State() {}
virtual ~State() {}
virtual void showMessage(Context *pContext) = 0;
};
class MessageOne :public State
{
public:
MessageOne() {}
~MessageOne() {}
void showMessage(Context *pContext)
{
printf("This is message one\n");
}
};
class MessageTwo :public State
{
public:
MessageTwo() {}
~MessageTwo() {}
void showMessage(Context *pContext)
{
printf("This is message two\n");
}
};
class MessageThree :public State
{
public:
MessageThree() {}
~MessageThree() {}
void showMessage(Context *pContext)
{
printf("This is message three\n");
}
};
class Context
{
public:
Context(State *pState) : m_pState(pState) {}
void Request()
{
if (m_pState)
{
m_pState->showMessage(this);
}
}
void ChangeState(State *pState)
{
m_pState = pState;
}
private:
State *m_pState;
};
int main()
{
State *pStateA = new MessageOne();
State *pStateB = new MessageTwo();
State *pStateC = new MessageThree();
Context *pContext = new Context(pStateA);
pContext->Request();
pContext->ChangeState(pStateB);
pContext->Request();
pContext->ChangeState(pStateC);
pContext->Request();
delete pContext;
delete pStateC;
delete pStateB;
delete pStateA;
return 0;
}
5. 多态+模板取代switch
#include
参考: JoannaJuanCV
C++ switch/case的替换方案
标签:tdm 过程 efault str case int ESS 带来 make
原文地址:https://www.cnblogs.com/yusq77/p/13926475.html