标签:cpp state oid 紧急 return pre 接口 复杂 div
核心: 把状态的判断逻辑转移到表示不同状态的一系列类当中,可以把复杂的判断逻辑简化
例子:
class State //状态接口 { public: virtual void show() = 0; }; class NormalState: public State //正常状态 { public: virtual void show() { cout << "Normal state, communicate success." << endl; } }; class CritcalState: public State //紧急状态 { public: virtual void show() { cout << "Critcal state, communicate failed." << endl; } };
class Communicate { public: void criticalState() //紧急状态下对应的操作 { CritcalState().show(); } void normalState() //正常状态下对应的操作 { NormalState().show(); } };
int main() { Communicate c; c.criticalState(); c.normalState(); return 0; }
标签:cpp state oid 紧急 return pre 接口 复杂 div
原文地址:https://www.cnblogs.com/chusiyong/p/11434203.html