行为模式之状态模式(State Pattern)C++实现

定义:允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。将状态封装成为独立的类,并将动作委托到代表当前状态的对象。将状态逻辑和动作实现进行分离。

状态模式类图:

行为模式之状态模式(State Pattern)C++实现_第1张图片

它与策略模式的类图一模一样,但是它们的差别在于“意图”。

它和程序状态机(PSM)不同,状态模式用类代表状态。Context会将行为委托给当前状态对象。状态模式允许Context随着状态的改变而改变行为。状态转换可以由State类或Context类控制。使用状态模式通常会导致设计中类的数目大量增加。状态类可以被多个Context实例共享。


状态模式C++实现如下:

State.h

//state.h
#ifndef _STATE_H_
#define _STATE_H_
class Context; //前置声明
class State
{
public:
	State();
	virtual ~State();
	virtual void OperationInterface(Context* )= 0;
	virtual void OperationChangeState(Context*) = 0;
protected:
	bool ChangeState(Context* con,State* st);
};


class ConcreteStateA:public State
{
public:
ConcreteStateA();
virtual ~ConcreteStateA();
virtual void OperationInterface(Context* );
virtual void OperationChangeState(Context*);
};
class ConcreteStateB:public State
{
public:
ConcreteStateB();
virtual ~ConcreteStateB();
virtual void OperationInterface(Context* );
virtual void OperationChangeState(Context*);
};


#endif 

State.CPP

#include "State.h"
#include "Context.h"
#include <iostream>
using namespace std;
State::State()
{
}
State::~State()
{
}
void State::OperationInterface(Context* con)
{
cout<<"State::.."<<endl;
}
void State::OperationChangeState(Context* con)
{
}
bool State::ChangeState(Context* con,State* st)
{
  con->ChangeState(st); 
  return true;
}


ConcreteStateA::ConcreteStateA()
{
}
ConcreteStateA::~ConcreteStateA()
{
}
void ConcreteStateA::OperationInterface(Context* con)
{
cout<<"ConcreteStateA::OperationInterface......"<<endl;
OperationChangeState(con);
}

void ConcreteStateA::OperationChangeState(Context* con)
{
this->ChangeState(con,new ConcreteStateB());
}



ConcreteStateB::ConcreteStateB()
{
}
ConcreteStateB::~ConcreteStateB()
{
}

void ConcreteStateB::OperationInterface(Context* con)
{
  cout<<"ConcreteStateB::OperationInterface......"<<endl;
  OperationChangeState(con);
}

void ConcreteStateB::OperationChangeState(Context* con)
{
 this->ChangeState(con,new ConcreteStateA());
}

Context.h

#ifndef _CONTEXT_H_
#define _CONTEXT_H_
class State;
class Context
{
public:
Context();
Context(State* state);
~Context();
void OprationInterface();
void OperationChangState();
protected:
friend class State; //表明在State类中可以访问Context类的private字段
bool ChangeState(State* state);
private:
	State* _state;
};


#endif

Context.CPP

#include "Context.h"
#include "State.h"
Context::Context()
{
}
Context::Context(State* state)
{
this->_state = state;
}
Context::~Context()
{
 delete _state;
}
void Context::OprationInterface()
{
_state->OperationInterface(this);
}
bool Context::ChangeState(State* state)
{
 this->_state = state;
return true;
}
void Context::OperationChangState()
{
  _state->OperationChangeState(this);
}

主函数main.CPP

#include "Context.h"
#include "State.h"
#include <iostream>
using namespace std;
int main(int argc,char* argv[])
{
State* st = new ConcreteStateA();
Context* con = new Context(st);

con->OprationInterface();
con-> OprationInterface ();
con->OprationInterface();
con->OprationInterface();
if (con != NULL)
delete con;
if (st != NULL)
st = NULL;
return 0;



}


复制 搜索

你可能感兴趣的:(C++,c,null,delete,Class)