状态机(state machine)
概念
状态机是一种行为型设计模式,用于在对象的内部状态改变时改变其行为。状态机通常用于模拟系统的状态转换以及在不同状态下的行为。
组成
状态机由++状态(state)、转换(transition)和上下文(context)组成++。上下文持有当前状态,并负责根据输入触发状态转换。
应用场景
状态机适用于需要根据对象内部状态的改变来改变其行为。例如:游戏角色的状态(站立、行走、跳跃等),网络协议的状态机等;
实现
使用直接编码转换逻辑,使用状态模式;
状态模式(state pattern)
概念
状态模式是一种结构型设计模式,它允许对象在其内部状态改变时改变其行为。状态模式通过将状态的封装和行为的委托来实现。
组成
状态模式通常包括++环境类(context)、抽象状态类(state)和具体状态类(concrete state)组成++。++环境类用于持有当前状态;具体状态类用于实现特定状态下的行为。++
应用场景
状态模式状态数不是固定的,++它使得状态的转换逻辑和状态的行为分离,便于维护和扩展。++
实例
context head类
#pragma once
#include "stateModel.h"
#include <iostream>
#include <memory.h>
using namespace std;
class WorkContext{
public:
std::shared_ptr<State> getState();
void setState(std::shared_ptr<State> state);
void requst() ;
private:
std::shared_ptr<State> mWorkState;
};
state head类
#pragma once
#include <iostream>
#include <cassert>
using namespace std;
class WorkContext;
enum EState {
WAKEUP = -1,
FORENOON ,
NOON,
EVENING
};
class State {
public:
State();
virtual ~State();
virtual void Handle() = 0;
protected:
EState mState;
std::shared_ptr<WorkContext> mWorkContext;
};
class WakeupState : public State {
public:
WakeupState();
void Handle();
};
class ForenoonState : public State {
public:
ForenoonState() ;
void Handle() ;
};
class NoonState : public State {
public:
NoonState () ;
void Handle() ;
};
class EveningState : public State {
public:
EveningState();
void Handle();
};
cpp类
#include "stateModel.h"
#include "context.h"
#include <memory.h>
std::shared_ptr<State> WorkContext::getState()
{
return mWorkState;
}
void WorkContext::setState(std::shared_ptr<State> state)
{
mWorkState = state;
}
void WorkContext::requst()
{
mWorkState->Handle();
}
State::State() { mWorkContext = std::make_shared<WorkContext>(); }
State::~State() {
}
WakeupState::WakeupState()
{
mState = WAKEUP;
}
void WakeupState::Handle()
{
if (mState <= WAKEUP)
{
std::cout << "current time is 6 am." << std::endl;
}
}
ForenoonState::ForenoonState()
{
std::cout << "forenoon state" << std::endl;
mState = FORENOON;
}
void ForenoonState::Handle() {
if (mState < NOON) {
std::cout << "current time is 10 am. " << std::endl;
} else {
mWorkContext->setState(std::shared_ptr<NoonState>());
mWorkContext->requst();
}
}
NoonState::NoonState () {
mState = NOON;
}
void NoonState::Handle() {
if (mState < EVENING) {
std::cout << "current time is 12 am." << std::endl;
} else {
mWorkContext->setState(std::shared_ptr<EveningState>(new EveningState));
mWorkContext->requst();
}
}
EveningState::EveningState() {
mState = EVENING;
}
void EveningState::Handle() {
if (mState > FORENOON) {
std::cout << "current time is eveing 10 pm." << std::endl;
} else {
mWorkContext->setState(std::shared_ptr<WakeupState>(new WakeupState));
mWorkContext->requst();
}
}
main.cpp
WorkContext workContext;
workContext.setState(std::shared_ptr<ForenoonState>(new ForenoonState));
workContext.requst();
workContext.setState(std::shared_ptr<NoonState>(new NoonState));
workContext.requst();
workContext.setState(std::shared_ptr<EveningState>(new EveningState));
workContext.requst();
workContext.setState(std::shared_ptr<WakeupState>(new WakeupState));
workContext.requst();
总结
状态模式是一种实现状态机的手法;状态机的跳转类(transition)主要是为了描述状态之间的转换关系,之间的关系拓扑图是上下文类(context)状态切换的依据。。。