Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2630967
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: 项目管理

2012-09-30 14:23:26

statepattern.h

点击(此处)折叠或打开

  1. /*
  2.  * statepattern.h
  3.  *
  4.  * Created on: Sep 26, 2012
  5.  * Author: hl
  6.  */

  7. #ifndef _STATEPATTERN_H_
  8. #define _STATEPATTERN_H_

  9. /**
  10.  * @brief 电梯动作接口和状态定义
  11.  */
  12. class ILift
  13. {
  14. public:
  15.     virtual void setState(int state) = 0;
  16. public:
  17.     virtual void open() = 0;
  18.     virtual void close() = 0;
  19.     virtual void run() = 0;
  20.     virtual void stop() = 0;
  21. public:
  22.     const static int OPENING_STATE = 1;    /// < 类似java的 public final static int xx = 1;
  23.     const static int CLOSING_STATE = 2;
  24.     const static int RUNNING_STATE = 3;
  25.     const static int STOPPING_STATE = 4;
  26. public:
  27.     const static int WARNING_1 = 1;        /// < 打开状态下不能运行
  28.     const static int WARNING_2 = 2;        /// < 运行状态下不能开门
  29. };

  30. /**
  31.  * @brief 电梯类 - 利用状态的判断(switch case)来实现逻辑结构!代码多了...
  32.  */
  33. class Lift : public ILift
  34. {
  35. public:
  36.     void setState(int state);
  37. public:
  38.     void open();    /// < 打开电梯
  39.     void close();    /// < 关闭电梯
  40.     void run();        /// < 启动电梯
  41.     void stop();    /// < 停止电梯
  42. private:
  43.     void openWithoutLogic();    /// < 打开电梯
  44.     void closeWithoutLogic();    /// < 关闭电梯
  45.     void runWithoutLogic();        /// < 启动电梯
  46.     void stopWithoutLogic();    /// < 停止电梯
  47. private:
  48.     void Waring(int errno);
  49. private:
  50.     int State;
  51. };


  52. /// < -------------真正的状态模式--------------------------


  53. class Context;
  54. /**
  55.  * @brief 电梯动作接口和状态定义
  56.  */
  57. class LiftState
  58. {
  59. public:
  60.     ~LiftState()
  61.     {
  62.         if (context)
  63.             delete context;
  64.     }
  65. public:
  66.     void setContext(Context * _context)
  67.     {
  68.         this->context = _context;
  69.     }
  70. public:
  71.     virtual void open() = 0;
  72.     virtual void close() = 0;
  73.     virtual void run() = 0;
  74.     virtual void stop() = 0;
  75. protected:
  76.     Context * context;
  77. };

  78. /**
  79.  * @brief 开门状态
  80.  */
  81. class OpenState : public LiftState
  82. {
  83. public:
  84.     void open();
  85.     void close();
  86.     void run();
  87.     void stop();
  88. };

  89. /**
  90.  * @brief 关闭状态 - 其他状态类似
  91.  */
  92. class CloseState : public LiftState
  93. {
  94. public:
  95.     void open();
  96.     void close();
  97.     void run();
  98.     void stop();
  99. };

  100. /**
  101.  * @brief 运行环境
  102.  */
  103. class Context
  104. {
  105. public:
  106.     ~Context()
  107.     {
  108.         if (liftState)
  109.             delete liftState;
  110.         if (openningState)
  111.             delete openningState;
  112.         if (closeingState)
  113.             delete closeingState;
  114.     }
  115. public:
  116.     LiftState * getLiftState() {
  117.         return liftState;
  118.     }
  119.     void setLiftState(LiftState * liftState) {
  120.         this->liftState = liftState;
  121.         //把当前的环境通知到各个实现类中
  122.         this->liftState->setContext(this);
  123.     }
  124.     void open()
  125.     {
  126.         this->liftState->open();
  127.     }
  128.     void close()
  129.     {
  130.         this->liftState->close();
  131.     }
  132.     void run()
  133.     {
  134.         this->liftState->run();
  135.     }
  136.     void stop()
  137.     {
  138.         this->liftState->stop();
  139.     }
  140. public:
  141.     static OpenState * openningState;
  142.     static CloseState * closeingState;
  143. private:
  144.     LiftState * liftState;
  145. };

  146. #endif /* _STATEPATTERN_H_ */
statepattern.cpp

点击(此处)折叠或打开

  1. //============================================================================
  2. // Name : statepattern.cpp
  3. // Author : hl
  4. // Version :
  5. // Copyright : Copyright (c) 2012 Tiros
  6. // Description : State Pattern in C++, Ansi-style
  7. //============================================================================

  8. #include "statepattern.h"
  9. #include <iostream>
  10. using namespace std;

  11. void Lift::setState(int state)
  12. {
  13.     State = state;
  14. }

  15. void Lift::open()
  16. {
  17.     switch(State)
  18.     {
  19.     case OPENING_STATE:
  20.         break;
  21.     case CLOSING_STATE:        /// < 如果关闭,则可以打开;同时修改状态为打开状态.
  22.         this->openWithoutLogic();
  23.         this->setState(OPENING_STATE);
  24.         break;
  25.     case RUNNING_STATE:
  26.         Waring(WARNING_2);
  27.         break;
  28.     case STOPPING_STATE:        /// < 如果停止的,则可以打开;同时修改状态为打开状态.
  29.         this->openWithoutLogic();
  30.         this->setState(OPENING_STATE);
  31.         break;
  32.     default:
  33.         break;
  34.     }
  35. }

  36. void Lift::close()
  37. {
  38.     switch (State)
  39.     {
  40.     case OPENING_STATE:        /// < 如果打开的,则可以关闭;同时修改状态为关闭状态.
  41.         this->closeWithoutLogic();
  42.         this->setState(CLOSING_STATE);
  43.         break;
  44.     case CLOSING_STATE:
  45.         break;
  46.     case RUNNING_STATE:
  47.         break;
  48.     case STOPPING_STATE:
  49.         break;
  50.     default:
  51.         break;
  52.     }
  53. }

  54. void Lift::run()
  55. {
  56.     switch (State)
  57.     {
  58.     case OPENING_STATE:
  59.         Waring(WARNING_1);
  60.         break;
  61.     case CLOSING_STATE:        /// < 如果关闭,则可以运行;同时修改状态为运行状态.
  62.         this->runWithoutLogic();
  63.         this->setState(RUNNING_STATE);
  64.         break;
  65.     case RUNNING_STATE:
  66.         break;
  67.     case STOPPING_STATE:    /// < 如果停止的,则可以运行;同时修改状态为运行状态.
  68.         this->runWithoutLogic();
  69.         this->setState(RUNNING_STATE);
  70.         break;
  71.     default:
  72.         break;
  73.     }
  74. }

  75. void Lift::stop()
  76. {
  77.     switch (State)
  78.     {
  79.     case OPENING_STATE:
  80.         break;
  81.     case CLOSING_STATE:        /// < 如果关闭,则可以停止;同时修改状态为停止状态.
  82.         this->stopWithoutLogic();
  83.         this->setState(STOPPING_STATE);
  84.         break;
  85.     case RUNNING_STATE:        /// < 如果运行,则可以停止;同时修改状态为停止状态.
  86.         this->stopWithoutLogic();
  87.         this->setState(STOPPING_STATE);
  88.         break;
  89.     case STOPPING_STATE:
  90.         break;
  91.     default:
  92.         break;
  93.     }
  94. }

  95. void Lift::openWithoutLogic()
  96. {
  97.     cout << "打开门" << endl;
  98. }

  99. void Lift::closeWithoutLogic()
  100. {
  101.     cout << "关门" << endl;
  102. }

  103. void Lift::runWithoutLogic()
  104. {
  105.     cout << "运行" << endl;
  106. }

  107. void Lift::stopWithoutLogic()
  108. {
  109.     cout << "停止" << endl;
  110. }

  111. void Lift::Waring(int errno)
  112. {
  113.     switch(errno)
  114.     {
  115.     case WARNING_1:
  116.         cout << "打开状态下不能运行" << endl;
  117.         break;
  118.     case WARNING_2:
  119.         cout << "运行状态下不能开门" << endl;
  120.         break;
  121.     default:
  122.         break;
  123.     }
  124. }


  125. /// < --------------------------------------

  126. OpenState * Context::openningState = new OpenState();
  127. CloseState * Context::closeingState = new CloseState();

  128. /**
  129.  * @brief 开门状态 - 可以关闭
  130.  */
  131. void OpenState::open()
  132. {
  133.     cout << "电梯门开启..." << endl;
  134. }

  135. void OpenState::close()
  136. {
  137.     context->setLiftState(context->closeingState);
  138.     context->getLiftState()->close();
  139. }

  140. void OpenState::run()
  141. {

  142. }

  143. void OpenState::stop()
  144. {

  145. }

  146. /**
  147.  * @brief 关闭状态 - 可以打开
  148.  */
  149. void CloseState::open()
  150. {
  151.     context->setLiftState(context->openningState);    /// < 设置当前电梯状态为openning状态
  152.     context->getLiftState()->open();                /// < OpenState's liftState打开
  153. }

  154. void CloseState::close()
  155. {
  156.     cout << "电梯门关闭..." << endl;
  157. }

  158. void CloseState::run()    /// < 设置为运行状态;
  159. {

  160. }

  161. void CloseState::stop()    /// < 设置为运行状态;
  162. {

  163. }





  164. int main()
  165. {
  166.     ILift * lif = new Lift();
  167.     lif->setState(ILift::CLOSING_STATE);    /// < 初始为关闭状态

  168.     lif->open();
  169.     lif->run();     /// < Warning1
  170.     lif->close();
  171.     lif->run();
  172.     lif->open();    /// < Warning2
  173.     lif->stop();
  174.     /*
  175.     打开门
  176.     打开状态下不能运行
  177.     关门
  178.     运行
  179.     运行状态下不能开门
  180.     停止
  181.     */

  182.     cout << "---------真正的状态模式-------------------\n";
  183.     Context cont;
  184.     cont.setLiftState(new CloseState());

  185.     cont.close();
  186.     cont.open();
  187.     cont.close();

  188.     return 0;
  189. }

阅读(1604) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~