state 状态模式
来源:互联网 发布:java中九九乘法表 编辑:程序博客网 时间:2024/06/03 21:33
#include <iostream>#include <string>using namespace std;/*1) 将 State声明为 Context的友元类(friend class) ,其作用是让 State模式访问 Context的 protected接口 ChangeSate() 。2) State 及其子类中的操作都将 Context*传入作为参数,其主要目的是 State 类可以通过这个指针调用 Context中的方法 (在本示例代码中没有体现) 。 这也是 State模式和 Strategy模式的最大区别所在。*/class state;class context{public: context():_st(NULL){} context(state *st):_st(st){} ~context(){ delete _st; } void operation_interface();private: friend class state; void operation_change_state(); bool change_state(state *st) { if (_st != NULL) delete _st; _st = st; return true; }private: state *_st;};class state{public: state(){}; virtual ~state(){}; virtual void operation_interface(context *) =0; virtual void operation_change_state(context *) =0;protected: bool change_state(context *con, state *st) { con->change_state(st); }};void context::operation_interface(){ _st->operation_interface(this); operation_change_state();}void context::operation_change_state(){ _st->operation_change_state(this);}class concrete_stateA : public state{public: concrete_stateA(){} ~concrete_stateA(){} virtual void operation_interface(context *con) { cout<< "concrete_stateA::operation_interface"<<endl; } void operation_change_state(context *con);};class concrete_stateB : public state{public: concrete_stateB(){} ~concrete_stateB(){} virtual void operation_interface(context *con) { cout<< "concrete_stateB::operation_interface"<<endl; } virtual void operation_change_state(context *con) { cout<< "concrete_stateB::operation_change_state"<<endl; this->change_state(con, new concrete_stateA); }};void concrete_stateA::operation_change_state(context *con){ cout<< "concrete_stateA::operation_change_state"<<endl; this->change_state(con, new concrete_stateB);}int main(){ state *st = new concrete_stateA; context *con = new context(st); con->operation_interface(); con->operation_interface(); con->operation_interface(); delete con; return 0;}////////////////////////////#include <iostream>#include <string>using namespace std;class context;class state{public: ~state(){} virtual string name() =0; virtual void handle(context *) =0;};class context{public: context(state *st):m_pst(st){} ~context(){ set_state(NULL); } void set_state(state *st) { if (m_pst != NULL) delete m_pst; m_pst = st; } state *get_state() { return m_pst; } void request() { cout<< m_pst->name() <<endl; m_pst->handle(this); cout<< m_pst->name() <<endl; }private: state *m_pst;};class concrete_stateA : public state{public: void handle(context *con); string name() { return "concrete_stateA"; }};class concrete_stateB : public state{public: void handle(context *con) { con->set_state(new concrete_stateA); } string name() { return "concrete_stateB"; }};void concrete_stateA::handle(context *con){ con->set_state(new concrete_stateB);}int main(){ context * c = new context(new concrete_stateA); c->request(); c->request(); c->request(); c->request(); c->request(); delete c; return 0;}
0 0
- State模式 状态模式
- 状态模式(State Pattern)
- 状态(State)模式
- 状态模式(State Pattern)
- 状态模式(State)
- 状态模式(State)
- State(状态)模式
- 状态模式(State)
- State 状态模式
- 状态模式(State Pattern)
- 状态模式(State)
- State(状态模式)
- Java 状态模式(State)
- 17.状态模式--State
- State(状态)模式
- 状态模式(State Pattern)
- 状态(state)模式
- 状态(State)模式
- android中layout属性
- c#调用c++写成的dll文件,返回char*,返回数组,用ref接收,byte[] (zz)
- UBOOT之main.c分析
- 无法嵌入互操作类型“MSScriptControl.ScriptControlClass”
- js编写ajax
- state 状态模式
- 主成分分析(PCA)学习有感
- 【协议分析】Wireshark 过滤表达式实例
- UBOOT之board.c分析(一)
- 支持向量机
- Why the “MAVEN2_CLASSPATH_CONTAINER will not be exported or published”
- strategy 策略模式
- jquery计算出left和top,让一个div水平垂直居中
- UBOOT之board.c分析(二)