add State Pattern

master
FengJungle 2019-11-09 10:41:43 +08:00
parent 3b7a291d50
commit ba025cb044
10 changed files with 427 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

View File

@ -0,0 +1,76 @@
#ifndef __DEMO_H__
#define __DEMO_H__
// 抽象状态类
class State
{
public:
// 声明抽象方法
virtual void handle() = 0;
};
// 具体状态类
class ConcreteState :public State
{
public:
// 实现
void handle(){
// ……
}
};
// 上下文类
class Context
{
public:
// set方法设置状态对象
void setState(State* iState){
this->state = iState;
}
// 对外封装的方法
void request(){
// do something
state->handle();
}
private:
// 引用状态对象
State *state;
};
// 1.由环境类来决定实例化为哪一个具体状态类对象
class Context
{
public:
void convertState(){
if (condition1){
this->state = new ConcreteStateA();
}
else if (condition2){
this->state = new ConcreteStateB();
}
else{
// do something
}
}
private:
// 引用状态对象
State *state;
};
// 2.有具体状态类来决定转换成哪一个具体状态类对象
class ConcreteState :public State
{
public:
void convertState(Context* ctx){
if (condition1){
ctx->setState(new ConcreteStateA());
}
else if (condition2){
ctx->setState(new ConcreteStateB());
}
else{
// do something
}
}
};
#endif //__DEMO_H__

View File

@ -0,0 +1,28 @@
#ifndef __GAMEACCOUNT_H__
#define __GAMEACCOUNT_H__
using namespace std;
#include <iostream>
// Ç°ÏòÉùÃ÷
class Level;
class GameAccount
{
public:
GameAccount();
GameAccount(string iName);
string getName();
void win();
void lose();
void playCard();
void setLevel(Level*);
int getScore();
void setScore(int);
private:
Level* level;
int score;
string name;
};
#endif

View File

@ -0,0 +1,66 @@
#include "GameAccount.h"
#include "Level.h"
#include <Windows.h>
#include <time.h>
#define random(x) (rand()%x)
GameAccount::GameAccount(){
printf("创立游戏角色积分100级别PRIMARY\n");
score = 100;
name = "none";
setLevel(new Primary(this));
}
GameAccount::GameAccount(string iName){
printf("创立游戏角色积分100级别PRIMARY\n");
score = 100;
name = iName;
setLevel(new Primary(this));
}
void GameAccount::setLevel(Level* iLevel){
this->level = iLevel;
}
string GameAccount::getName(){
return name;
}
void GameAccount::playCard(){
this->level->playCard();
Sleep(100);
srand((int)time(0));
int res = random(2);
if (res % 2 == 0){
this->win();
}
else{
this->lose();
}
this->level->upgradeLevel();
}
void GameAccount::win(){
if (this->getScore() < 200){
setScore(getScore() + 50);
}
else{
setScore(getScore() + 100);
}
printf("\n\t胜利,最新积分为 %d\n", score);
}
void GameAccount::lose(){
setScore(getScore() + 30);
printf("\n\t输牌,最新积分为 %d\n", score);
}
int GameAccount::getScore(){
return this->score;
}
void GameAccount::setScore(int iScore){
this->score = iScore;
}

View File

@ -0,0 +1,158 @@
#include "Level.h"
/*********** Level *************/
Level::Level(){
}
void Level::playCard(){
this->play();
this->doubleScore();
this->changeCards();
this->peekCards();
}
void Level::play(){
printf("\t使用基本技能,");
}
void Level::setGameAccount(GameAccount* iGameAccount){
this->gameAccount = iGameAccount;
}
GameAccount* Level::getGameAccount(){
return gameAccount;
}
/*********** Primary *************/
Primary::Primary(){
}
Primary::Primary(GameAccount* iGameAccount){
this->setGameAccount(iGameAccount);
}
Primary::Primary(Level* level){
getGameAccount()->setLevel(level);
}
void Primary::doubleScore(){
return;
}
void Primary::changeCards(){
return;
}
void Primary::peekCards(){
return;
}
void Primary::upgradeLevel(){
if (this->getGameAccount()->getScore() > 150){
this->getGameAccount()->setLevel(new Secondary(this));
printf("\t升级! 级别SECONDARY\n\n");
}
else{
printf("\n");
}
}
/*********** Secondary *************/
Secondary::Secondary(){
}
Secondary::Secondary(Level* level){
this->setGameAccount(level->getGameAccount());
getGameAccount()->setLevel(level);
}
void Secondary::doubleScore(){
printf("使用胜利双倍积分技能");
}
void Secondary::changeCards(){
return;
}
void Secondary::peekCards(){
return;
}
void Secondary::upgradeLevel(){
if (this->getGameAccount()->getScore() < 150){
this->getGameAccount()->setLevel(new Primary(this));
printf("\t降级! 级别PRIMARY\n\n");
}
else if (this->getGameAccount()->getScore() > 200){
this->getGameAccount()->setLevel(new Professional(this));
printf("\t升级! 级别PROFESSIONAL\n\n");
}
}
/*********** Professional *************/
Professional::Professional(){
}
Professional::Professional(Level* level){
this->setGameAccount(level->getGameAccount());
getGameAccount()->setLevel(level);
}
void Professional::doubleScore(){
printf("使用胜利双倍积分技能,");
}
void Professional::changeCards(){
printf("使用换牌技能");
}
void Professional::peekCards(){
return;
}
void Professional::upgradeLevel(){
if (this->getGameAccount()->getScore() < 200){
this->getGameAccount()->setLevel(new Secondary(this));
printf("\t降级! 级别SECONDARY\n\n");
}
else if (this->getGameAccount()->getScore() > 250){
this->getGameAccount()->setLevel(new Final(this));
printf("\t升级! 级别FINAL\n\n");
}
}
/*********** Final *************/
Final::Final(){
}
Final::Final(Level* level){
this->setGameAccount(level->getGameAccount());
getGameAccount()->setLevel(level);
}
void Final::doubleScore(){
printf("使用胜利双倍积分技能,");
}
void Final::changeCards(){
printf("使用换牌技能,");
}
void Final::peekCards(){
printf("使用偷看卡牌技能");
}
void Final::upgradeLevel(){
if (this->getGameAccount()->getScore() < 250){
this->getGameAccount()->setLevel(new Professional(this));
printf("\t降级! 级别PROFESSIONAL\n\n");
}
else{
printf("\t%s 已经是最高级\n\n", this->getGameAccount()->getName().c_str());
}
}

View File

@ -0,0 +1,78 @@
#ifndef __LEVEL_H__
#define __LEVEL_H__
#include "GameAccount.h"
class Level
{
public :
Level();
// 声明方法
void playCard();
void play();
virtual void doubleScore() = 0;
virtual void changeCards() = 0;
virtual void peekCards() = 0;
// 升级
virtual void upgradeLevel() = 0;
GameAccount* getGameAccount();
void setGameAccount(GameAccount* iGameAccount);
private:
GameAccount* gameAccount;
};
class Primary;
class Secondary;
class Professional;
class Final;
class Primary :public Level
{
public:
Primary();
Primary(Level* level);
Primary(GameAccount* ga);
void doubleScore();
void changeCards();
void peekCards();
// 升级
void upgradeLevel();
};
class Secondary :public Level
{
public:
Secondary();
Secondary(Level* level);
void doubleScore();
void changeCards();
void peekCards();
// 升级
void upgradeLevel();
};
class Professional :public Level
{
public:
Professional();
Professional(Level* level);
void doubleScore();
void changeCards();
void peekCards();
// 升级
void upgradeLevel();
};
class Final :public Level
{
public:
Final();
Final(Level* level);
void doubleScore();
void changeCards();
void peekCards();
// 升级
void upgradeLevel();
};
#endif

View File

@ -0,0 +1,16 @@
#include "GameAccount.h"
#include "Level.h"
int main()
{
GameAccount *jungle = new GameAccount("Jungle");
for (int i = 0; i < 5; i++){
printf("第%d局\n", i + 1);
jungle->playCard();
}
printf("\n\n");
system("pause");
return 0;
}

View File

@ -93,4 +93,8 @@ Jungle设计模式系列
23.设计模式(二十三)——观察者模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102927937
博客地址https://blog.csdn.net/sinat_21107433/article/details/102927937
24.设计模式(二十四)——状态模式
博客地址https://blog.csdn.net/sinat_21107433/article/details/102966121