我与C++设计模式(十四)——状态模式

来源:互联网 发布:物理怎么才能学好 知乎 编辑:程序博客网 时间:2024/06/05 11:18

state模式实现了状态逻辑和动作实现的分离。比如一个有限状态机,对不同的输入有不同的响应,通常我们用switch-case语句来完成,当我们的状态很多,case语句也就跟着扩展,然而,当case语句增多也对逻辑维护带来了一定的难度,这种设计无疑是低效难维护的。

通过state模式,将状态逻辑和动作实现分离将很好地解决这一问题,我们可以每个状态分支封装成一个类,状态的转换也交给类实现,并将所有的类抽象出一个接口,供另一个类context来完成切换调用。见UML图:


代码如下:

#ifndef _STATE_H__#define _STATE_H__class context;class state{        public:                state();                virtual ~state();                virtual void operation_interface(context *) = 0;                virtual void operation_change_state(context *) = 0;        protected:                bool change_state(context*,state*);};class concrete_state_A:public state{        public:                concrete_state_A();                ~concrete_state_A();                void operation_interface(context *);                void operation_change_state(context *);};class concrete_state_B:public state{        public:                concrete_state_B();                ~concrete_state_B();                void operation_interface(context *);                void operation_change_state(context *);};#endif

//state.cpp#include "state.h"#include "context.h"#include <iostream>using namespace std;state::state(){}state::~state(){}void state::operation_interface(context *p_con){        cout<<"state::..."<<endl;}bool state::change_state(context *p_con,state *p_sta){        p_con->change_state(p_sta);        return true;}void state::operation_change_state(context *p_con){}concrete_state_A::concrete_state_A(){}concrete_state_A::~concrete_state_A(){}void concrete_state_A::operation_interface(context *p_con){        cout<<"concrete_state_A::..."<<endl;}void concrete_state_A::operation_change_state(context *p_con){        operation_interface(p_con);        change_state(p_con,new concrete_state_B());}concrete_state_B::concrete_state_B(){}concrete_state_B::~concrete_state_B(){}void concrete_state_B::operation_interface(context * p_con){        cout<<"concrete_state_B::..."<<endl;}void concrete_state_B::operation_change_state(context *p_con){        operation_interface(p_con);        change_state(p_con,new concrete_state_A());}

------------context--------------------

#ifndef _CONTEXT_H__#define _CONTEXT_H__class state;class context{        public:                context();                context(state *);                ~context();                void operation_interface();                void operation_change_state();        private:                friend class state;                bool change_state(state *p_sta);                state *_p_sta;};#endif

//context.cpp#include "context.h"#include "state.h"context::context(){}context::context(state *p_sta)        :_p_sta(p_sta){}context::~context(){        delete _p_sta;}void context::operation_interface(){        _p_sta->operation_interface(this);}bool context::change_state(state *p_sta){        _p_sta = p_sta;        return true;}void context::operation_change_state(){        _p_sta->operation_change_state(this);}

------------main---------

//main.cpp#include "state.h"#include "context.h"int main(int argc,char **argv){        state *p_sta = new concrete_state_A();        context *p_con = new context(p_sta);        p_con->operation_change_state();        p_con->operation_change_state();        p_con->operation_change_state();        if (p_con)                delete p_con;        if (p_sta)                delete p_sta;        return 0;}

ps:书中代码有误,额,我在说哪本儿书?不重要啦~

0 0