设计模式之Sate模式

State.h

#ifndef _STATE_H_
#define _STATE_H_
class CState;
class CContext
{
public:
	CContext();
	CContext(CState *_sta);
	~CContext();
	void OperationInterface();
	void OperationChangeState();
	bool ChangeState(CState *_sta);
private:
	friend class CState;
	CState	*m_sta;
};
class CState
{
public:
	CState();
	virtual ~CState();
	virtual void OperationInterface(CContext	*_con)=0;
	virtual void OperationChangeState(CContext	*_con)=0;
	bool ChangeState(CContext	*_con,CState *_sta);
};

class CConcreteStateA:public CState
{
public:
	CConcreteStateA();
	~CConcreteStateA();
	void OperationInterface(CContext	*_con);
	void OperationChangeState(CContext	*_con);
};

class CConcreteStateB:public CState
{
public:
	CConcreteStateB();
	~CConcreteStateB();
	void OperationInterface(CContext	*_con);
	void OperationChangeState(CContext	*_con);
};
#endif

State.cpp

#include <iostream>
#include "State.h"
using namespace std;

CContext::CContext(CState *_sta)
{
	if(_sta)
	{
		this->m_sta=_sta;
	}
}

CContext::CContext()
{

}

CContext::~CContext()
{
	if(m_sta)
	{
		delete m_sta;
		m_sta=0;
	}
}

bool CContext::ChangeState(CState *_sta)
{
	if(_sta)
	{
		this->m_sta=_sta;
		return true;
	}
	return false;
}

void CContext::OperationChangeState()
{
	if(m_sta)
		m_sta->OperationChangeState(this);
}

void CContext::OperationInterface()
{
	if(m_sta)
		m_sta->OperationInterface(this);

}

CState::CState()
{

}

CState::~CState()
{

}

bool CState::ChangeState(CContext *_con, CState *_sta)
{
	_con->ChangeState(_sta);
	return true;
}

void CState::OperationInterface(CContext	*_con)
{
	cout<<" CState::OperationInterface"<<endl;
}

void CState::OperationChangeState(CContext	*_con)
{

}
CConcreteStateA::CConcreteStateA()
{

}

CConcreteStateA::~CConcreteStateA()
{

}

void CConcreteStateA::OperationChangeState(CContext	*_con)
{
	OperationInterface(_con);
	ChangeState(_con,new CConcreteStateB());
}

void CConcreteStateA::OperationInterface(CContext	*_con)
{
	cout<<" CConcreteStateA::OperationInterface"<<endl;
}

CConcreteStateB::CConcreteStateB()
{

}

CConcreteStateB::~CConcreteStateB()
{

}

void CConcreteStateB::OperationChangeState(CContext	*_con)
{
	OperationInterface(_con);
	ChangeState(_con,new CConcreteStateA());
}

void CConcreteStateB::OperationInterface(CContext	*_con)
{
	cout<<" CConcreteStateB::OperationInterface"<<endl;
}

Main.cpp

#include <iostream>
#include "State.h"
using namespace std;

int main() //Client
{
	CState* st=new CConcreteStateA();
	CContext *con=new CContext(st);
	con->OperationChangeState();
	con->OperationChangeState();
	con->OperationChangeState();
	if(st)
	{
		delete st;
		st=0;
	}

	if(con)
	{
		delete con;
		con=0;
	}
	return 0;
}

State模式在实现中,有两个关键点:
1)将State声明为Context的友元类(friend class),其作用是让State模式访问Context的protected接口ChangeSate()。
2)State及其子类中的操作都将Context*传入作为参数,其主要目的是State类可以通过这个指针调用Context中的方法(在本示例代码中没有体现)。这也是State模式和Strategy模式的最大区别所在。
运行了示例代码后可以获得以下的结果:连续3次调用了Context的OprationInterface()因为每次调用后状态都会改变(A-B-A),因此该动作随着Context的状态的转变而获得了不同的结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值