当前位置:   article > 正文

大话设计模式16----状态模式_c# 大话设计模式 状态模式

c# 大话设计模式 状态模式

大话设计模式


1 状态模式(State)结构图




2 对状态模式的一些解释

概念:当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

  • 状态模式主要解决的是档控制一个对象状态转换的条件表达式过于复杂的情况。把状态的判断逻辑转移到表示不同状态的一些列类中,可以把复杂的逻辑简化;
  • 将特定的状态相关的行为都放入一个对象中,由于所有与状态相关的代码都存在于某个ConcreteState中,所以通过定义新的子类可以很容易的增加新的状态和转换;
  • 状态模式通过把各种状态转移逻辑分布到State的子类之间,来减少相互间的依赖;
  • 当一个对象的行为取决于它的状态,并且她必须在运行时刻根据状态改变它的行为时,就可以考虑使用状态模式了。


State类:
  1. class State
  2. {
  3. public:
  4. virtual void Handle(Context *context);
  5. };

ConcreteState:
  1. class ConcreteStateA :public State
  2. {
  3. public:
  4. void Handle(Context *context)
  5. {
  6. context->state = new ConcreteStateB();//设置下一个状态
  7. }
  8. };
  9. class ConcreteStateB :public State
  10. {
  11. public:
  12. void Handle(Context *context)
  13. {
  14. context->state = new ConcreteStateA();
  15. }
  16. };

Context类
  1. class Context
  2. {
  3. private:
  4. State *state;
  5. public:
  6. Context(State *state)//定义初始状态
  7. {
  8. this->state = state;
  9. }
  10. void setState(State *state)//设置当前的状态
  11. {
  12. this->state = state;
  13. }
  14. State *getState()//读取当前的状态
  15. {
  16. return this->state();
  17. }
  18. void Request()
  19. {
  20. state->Handle();//对当前状态下的请求做处理
  21. }
  22. };

客户端代码:
  1. int main()
  2. {
  3. Context *context = new Context(new ConcreteStateA());
  4. context->Request();
  5. }

3 C++代码实现


1 代码结构图:





2 C++源代码:


State.h
  1. #ifndef _STATE_H_
  2. #define _STATE_H_
  3. class Work;
  4. class State
  5. {
  6. public:
  7. virtual void writeProgram(Work *w);
  8. };
  9. class ForenoonState :public State
  10. {
  11. public:
  12. void writeProgram(Work *w) override;
  13. };
  14. class NoonState :public State
  15. {
  16. public:
  17. void writeProgram(Work *w) override;
  18. };
  19. class AfternoonState :public State
  20. {
  21. public:
  22. void writeProgram(Work *w) override;
  23. };
  24. class EveningState :public State
  25. {
  26. public:
  27. void writeProgram(Work *w) override;
  28. };
  29. class SleepingState :public State
  30. {
  31. public:
  32. void writeProgram(Work *w) override;
  33. };
  34. class RestState :public State
  35. {
  36. public:
  37. void writeProgram(Work *w) override;
  38. };
  39. #endif

State.cpp
  1. #include"State.h"
  2. #include"Work.h"
  3. #include<iostream>
  4. using std::cout;
  5. using std::endl;
  6. void State::writeProgram(Work *w)
  7. {
  8. cout << "开始写程序" << endl;
  9. }
  10. void ForenoonState::writeProgram(Work *w)
  11. {
  12. if (w->getHour() < 12)
  13. {
  14. cout << "时间:" << w->getHour() << "\t精神百倍!" << endl;
  15. }
  16. else
  17. {
  18. w->setState(new NoonState());
  19. w->writeProgram();
  20. }
  21. }
  22. void NoonState::writeProgram(Work *w)
  23. {
  24. if (w->getHour() < 13)
  25. {
  26. cout << "时间:" << w->getHour() << "\t中午困死了,能不能休息会!" << endl;
  27. }
  28. else
  29. {
  30. w->setState(new AfternoonState());
  31. w->writeProgram();
  32. }
  33. }
  34. void AfternoonState::writeProgram(Work *w)
  35. {
  36. if (w->getHour() < 17)
  37. {
  38. cout << "时间:" << w->getHour() << "\t下午的状态还是不错的,继续加油!" << endl;
  39. }
  40. else
  41. {
  42. w->setState(new EveningState());
  43. w->writeProgram();
  44. }
  45. }
  46. void EveningState::writeProgram(Work *w)
  47. {
  48. if (w->getFinish())
  49. {
  50. cout << "工作完成! ";
  51. w->setState(new RestState());
  52. w->writeProgram();
  53. }
  54. else
  55. {
  56. if (w->getHour() < 21)
  57. {
  58. cout << "时间:" << w->getHour() << "\t加班啊,可恶!" << endl;
  59. }
  60. else
  61. {
  62. w->setState(new SleepingState());
  63. w->writeProgram();
  64. }
  65. }
  66. }
  67. void SleepingState::writeProgram(Work *w)
  68. {
  69. cout << "时间:" << w->getHour() << "\t困得不行了,睡着了!" << endl;
  70. }
  71. void RestState::writeProgram(Work *w)
  72. {
  73. cout << "时间:" << w->getHour() << "\t下班回家!" << endl;
  74. }

Work.h
  1. #ifndef _WORK_H_
  2. #define _WORK_H_
  3. class State;
  4. class Work
  5. {
  6. private:
  7. State *state;
  8. double hour;
  9. bool finish;
  10. public:
  11. Work();
  12. void setState(State *state);
  13. State *getState();
  14. void setHour(double hour);
  15. double getHour();
  16. void setFinish(bool finish);
  17. bool getFinish();
  18. void writeProgram();
  19. };
  20. #endif

Work.cpp
  1. #include"Work.h"
  2. #include"State.h"
  3. Work::Work()
  4. {
  5. state = new ForenoonState();
  6. this->hour = 9;
  7. this->finish = false;
  8. }
  9. void Work::setState(State *state)
  10. {
  11. this->state = state;
  12. }
  13. State * Work::getState()
  14. {
  15. return this->state;
  16. }
  17. void Work::setHour(double hour)
  18. {
  19. this->hour = hour;
  20. }
  21. double Work::getHour()
  22. {
  23. return this->hour;
  24. }
  25. void Work::setFinish(bool finish)
  26. {
  27. this->finish = finish;
  28. }
  29. bool Work::getFinish()
  30. {
  31. return this->finish;
  32. }
  33. void Work::writeProgram()
  34. {
  35. this->state->writeProgram(this);
  36. }

客户端
  1. #include"State.h"
  2. #include"Work.h"
  3. #include<iostream>
  4. using namespace std;
  5. int main()
  6. {
  7. Work *work = new Work();
  8. work->setHour(9);
  9. work->writeProgram();
  10. work->setHour(12.5);
  11. work->writeProgram();
  12. work->setHour(16);
  13. work->writeProgram();
  14. work->setHour(20);
  15. work->writeProgram();
  16. work->setFinish(true);
  17. work->writeProgram();
  18. work->setHour(10);
  19. //时间重置,那么状态也应该重置一下
  20. work->setState(new ForenoonState());
  21. work->setFinish(false);
  22. work->writeProgram();
  23. work->setHour(22);
  24. work->writeProgram();
  25. system("pause");
  26. return 0;
  27. }

运行结果:
时间:9 精神百倍!
时间:12.5      中午困死了,能不能休息会!
时间:16        下午的状态还是不错的,继续加油!
时间:20        加班啊,可恶!
工作完成!  时间:20    下班回家!
时间:10        精神百倍!
时间:22        困得不行了,睡着了!
请按任意键继续. . .







声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/421473
推荐阅读
相关标签
  

闽ICP备14008679号