标签:
今天来学习一下设计模式中的状态模式。之前经常听说状态机之类的东东,自己也有用过,但是状态机和状态模式还是有一些区别,今天主要看一下状态模式的定义,例子,应用,最后再分析一下状态模式和我们所说的有限状态机之间到底是什么关系。
// Design Pattern.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; enum CharState { Invalid = -1, Idle, Walk, Attack, Did, }; class Character { private: CharState m_CurrentState; public: Character() : m_CurrentState(Idle) { } void SetState(CharState state) { m_CurrentState = state; PlayAnimation(); } void Tick() { switch (m_CurrentState) { case Invalid: break; case Idle: cout << "Idle Tick,无操作" << endl; break; case Walk: cout << "Walk Tick, 更改位置" << endl; break; case Attack: cout << "Attack Tick,释放技能" << endl; break; case Did: cout << "Die Tick, 销毁对象" << endl; break; default: break; } } void PlayAnimation() { switch (m_CurrentState) { case Invalid: break; case Idle: cout << "播放休闲动画" << endl; break; case Walk: cout << "播放行走动画" << endl; break; case Attack: cout << "播放攻击动画" << endl; break; case Did: cout << "播放死亡动画" << endl; break; default: break; } } }; int _tmain(int argc, _TCHAR* argv[]) { Character* man = new Character(); man->SetState(Attack); man->Tick(); man->SetState(Walk); man->Tick(); system("pause"); return 0; }结果如下:
// Design Pattern.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; //人物状态基类 class CharState { public: virtual void PlayAnimation() = 0; virtual void Tick() = 0; }; //休闲状态类 class IdleState : public CharState { public: void PlayAnimation(){ cout << "播放休闲动画" << endl; } void Tick(){ cout << "Idle Tick,无操作" << endl; } }; //行走状态类 class WalkState : public CharState { public: void PlayAnimation(){ cout << "播放行走动画" << endl; } void Tick(){ cout << "Walk Tick,修改位置" << endl; } }; //攻击状态类 class AttackState : public CharState { public: void PlayAnimation(){ cout << "播放攻击动画" << endl; } void Tick(){ cout << "Attack Tick,释放技能" << endl; } }; //死亡状态类 class DieState : public CharState { public: void PlayAnimation(){ cout << "播放死亡动画" << endl; } void Tick(){ cout << "Die Tick,销毁对象" << endl; } }; //人物类 class Character { private: CharState* m_CurrentState; public: Character() : m_CurrentState(NULL) { } void SetState(CharState* state) { m_CurrentState = state; PlayAnimation(); } void Tick() { m_CurrentState->Tick(); } void PlayAnimation() { m_CurrentState->PlayAnimation(); } }; int _tmain(int argc, _TCHAR* argv[]) { Character* man = new Character(); CharState* Idle = new IdleState(); CharState* Walk = new WalkState(); CharState* Attack = new AttackState(); CharState* Die = new DieState(); man->SetState(Attack); man->Tick(); man->SetState(Walk); man->Tick(); system("pause"); return 0; }结果如下:
man.SetState(new AttackState());
// Design Pattern.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; //人物状态基类 class CharState { public: virtual void PlayAnimation() = 0; virtual void Tick() = 0; }; //休闲状态类 class IdleState : public CharState { public: void PlayAnimation(){ cout << "播放休闲动画" << endl; } void Tick(){ cout << "Idle Tick,无操作" << endl; } }; //行走状态类 class WalkState : public CharState { public: void PlayAnimation(){ cout << "播放行走动画" << endl; } void Tick(){ cout << "Walk Tick,修改位置" << endl; } }; //攻击状态类 class AttackState : public CharState { public: void PlayAnimation(){ cout << "播放攻击动画" << endl; } void Tick(){ cout << "Attack Tick,释放技能" << endl; } }; //死亡状态类 class DieState : public CharState { public: void PlayAnimation(){ cout << "播放死亡动画" << endl; } void Tick(){ cout << "Die Tick,销毁对象" << endl; } }; //人物类 class Character { private: CharState* m_CurrentState; public: Character() : m_CurrentState(NULL) { } void SetState(CharState* state) { m_CurrentState = state; PlayAnimation(); } void Tick() { m_CurrentState->Tick(); } void PlayAnimation() { m_CurrentState->PlayAnimation(); } public: static CharState* Idle; static CharState* Walk; static CharState* Attack; static CharState* Die; }; CharState* Character::Idle = new IdleState(); CharState* Character::Walk = new WalkState(); CharState* Character::Attack = new AttackState(); CharState* Character::Die= new DieState(); int _tmain(int argc, _TCHAR* argv[]) { Character* man = new Character(); Character* woman = new Character(); man->SetState(Character::Attack); man->Tick(); man->SetState(Character::Die); man->Tick(); woman->SetState(Character::Walk); woman->Tick(); woman->SetState(Character::Idle); woman->Tick(); man->SetState(new AttackState()); system("pause"); return 0; }结果如下:
标签:
原文地址:http://blog.csdn.net/puppet_master/article/details/51254909