diff --git a/21.StatePattern/1.Picture/状态模式UML图.png b/21.StatePattern/1.Picture/状态模式UML图.png new file mode 100644 index 0000000..33d492f Binary files /dev/null and b/21.StatePattern/1.Picture/状态模式UML图.png differ diff --git a/21.StatePattern/1.Picture/状态模式举例.png b/21.StatePattern/1.Picture/状态模式举例.png new file mode 100644 index 0000000..8228851 Binary files /dev/null and b/21.StatePattern/1.Picture/状态模式举例.png differ diff --git a/21.StatePattern/1.Picture/状态模式实例UML图.png b/21.StatePattern/1.Picture/状态模式实例UML图.png new file mode 100644 index 0000000..ff071c5 Binary files /dev/null and b/21.StatePattern/1.Picture/状态模式实例UML图.png differ diff --git a/21.StatePattern/2.Code/Demo.h b/21.StatePattern/2.Code/Demo.h new file mode 100644 index 0000000..2280374 --- /dev/null +++ b/21.StatePattern/2.Code/Demo.h @@ -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__ \ No newline at end of file diff --git a/21.StatePattern/2.Code/GameAccount.h b/21.StatePattern/2.Code/GameAccount.h new file mode 100644 index 0000000..c29b60e --- /dev/null +++ b/21.StatePattern/2.Code/GameAccount.h @@ -0,0 +1,28 @@ +#ifndef __GAMEACCOUNT_H__ +#define __GAMEACCOUNT_H__ + +using namespace std; +#include +// ǰ +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 diff --git a/21.StatePattern/2.Code/GameAccout.cpp b/21.StatePattern/2.Code/GameAccout.cpp new file mode 100644 index 0000000..54f447c --- /dev/null +++ b/21.StatePattern/2.Code/GameAccout.cpp @@ -0,0 +1,66 @@ +#include "GameAccount.h" +#include "Level.h" +#include +#include +#define random(x) (rand()%x) + +GameAccount::GameAccount(){ + printf("Ϸɫ֣100PRIMARY\n"); + score = 100; + name = "none"; + setLevel(new Primary(this)); +} + +GameAccount::GameAccount(string iName){ + printf("Ϸɫ֣100PRIMARY\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; +} \ No newline at end of file diff --git a/21.StatePattern/2.Code/Level.cpp b/21.StatePattern/2.Code/Level.cpp new file mode 100644 index 0000000..b18bdc3 --- /dev/null +++ b/21.StatePattern/2.Code/Level.cpp @@ -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()); + } +} \ No newline at end of file diff --git a/21.StatePattern/2.Code/Level.h b/21.StatePattern/2.Code/Level.h new file mode 100644 index 0000000..a3ea573 --- /dev/null +++ b/21.StatePattern/2.Code/Level.h @@ -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 \ No newline at end of file diff --git a/21.StatePattern/2.Code/main.cpp b/21.StatePattern/2.Code/main.cpp new file mode 100644 index 0000000..99b8e48 --- /dev/null +++ b/21.StatePattern/2.Code/main.cpp @@ -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; +} \ No newline at end of file diff --git a/README.md b/README.md index ad640a6..a352a24 100644 --- a/README.md +++ b/README.md @@ -93,4 +93,8 @@ Jungle设计模式系列 23.设计模式(二十三)——观察者模式 -博客地址:https://blog.csdn.net/sinat_21107433/article/details/102927937 \ No newline at end of file +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102927937 + +24.设计模式(二十四)——状态模式 + +博客地址:https://blog.csdn.net/sinat_21107433/article/details/102966121 \ No newline at end of file