Chinaunix首页 | 论坛 | 博客
  • 博客访问: 170252
  • 博文数量: 60
  • 博客积分: 15
  • 博客等级: 民兵
  • 技术积分: 638
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-26 10:59
个人简介

喜欢coding,因为那是一件伟大的事情,是将无生命的IC赋予灵魂的过程,让我拥有了和上帝一样的成就感。(w1c2g3@163.com)

文章分类

全部博文(60)

文章存档

2017年(7)

2016年(41)

2015年(1)

2014年(4)

2013年(7)

我的朋友

分类: C/C++

2016-10-30 22:58:31

状态模式 允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。




  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>

  4. using namespace std;

  5. class GumballMachine;

  6. class State {
  7. public:
  8.     virtual insertQuarter() {
  9.         cout << "wrong state" << endl;
  10.     }
  11.     
  12.     virtual ejectQuarter() {
  13.         cout << "wrong state" << endl;
  14.     }
  15.     
  16.     virtual turnCrank() {
  17.         cout << "wrong state" << endl;
  18.     }
  19.     
  20.     virtual dispense() {
  21.         cout << "wrong state" << endl;
  22.     }
  23.     
  24.     virtual string printInfo() = 0;
  25.     State(GumballMachine *mgumballMachine) : gumballMachine(mgumballMachine) {}
  26. protected:
  27.     GumballMachine *gumballMachine;
  28. };

  29. class GumballMachine {
  30. public:
  31.     GumballMachine(int numberGumballs) {
  32.         soldOutState = new SoldOutState(this);
  33.         noQuarterState = new NoQuarterState(this);
  34.         hasQuarterState = new HasQuarterState(this);
  35.         soldState = new SoldState(this);

  36.         this->count = numberGumballs;
  37.          if (numberGumballs > 0) {
  38.             state = noQuarterState;
  39.         } else {
  40.             state = soldOutState;
  41.         }
  42.     }
  43.     
  44.      ~GumballMachine() {
  45.          delete soldOutState;
  46.          delete noQuarterState;
  47.          delete hasQuarterState;
  48.          delete soldState;
  49.     }
  50.     
  51.     void insertQuarter() {
  52.         state->insertQuarter();
  53.     }
  54.  
  55.     void ejectQuarter() {
  56.         state->ejectQuarter();
  57.     }
  58.  
  59.     void turnCrank() {
  60.         state->turnCrank();
  61.         state->dispense();
  62.     }

  63.     virtual void setState(State *state) {
  64.         this->state = state;
  65.     }
  66.  
  67.     virtual void releaseBall() {
  68.         cout << "A gumball comes rolling out the slot..." << endl;
  69.         if (count != 0) {
  70.             count = count - 1;
  71.         }
  72.     }
  73.  
  74.     virtual int getCount() {
  75.         return count;
  76.     }
  77.  
  78.     void refill(int count) {
  79.         this->count = count;
  80.         state = noQuarterState;
  81.     }

  82.     virtual State *getState() {
  83.         return state;
  84.     }

  85.     virtual State *getSoldOutState() {
  86.         return soldOutState;
  87.     }

  88.     virtual State *getNoQuarterState() {
  89.         return noQuarterState;
  90.     }

  91.     virtual State *getHasQuarterState() {
  92.         return hasQuarterState;
  93.     }

  94.     virtual State *getSoldState() {
  95.         return soldState;
  96.     }
  97.  
  98.     printInfo() {
  99.         string result;
  100.         result.append("\nMighty Gumball, Inc.");
  101.         result.append("\nC++-enabled Standing Gumball Model #2016");
  102.         result.append("\nInventory: ");
  103.         ostringstream oss;
  104.         oss << count;
  105.         result.append(oss.str());
  106.         result.append(" gumball");
  107.         if (count != 1) {
  108.             result.append("s");
  109.         }
  110.         result.append("\n");
  111.         result.append("Machine is ");
  112.         result.append(state->printInfo());
  113.         result.append("\n");
  114.         cout << result << endl;
  115.     }
  116.     
  117. private:
  118.     State *soldOutState;
  119.     State *noQuarterState;
  120.     State *hasQuarterState;
  121.     State *soldState;
  122.  
  123.     State *state;
  124.     int count;
  125.     
  126.     class NoQuarterState : public State {
  127.     public:
  128.      NoQuarterState(GumballMachine *gumballMachine) :
  129.             State(gumballMachine) {}
  130.     
  131.         virtual insertQuarter() {
  132.             cout << "You inserted a quarter" << endl;
  133.             gumballMachine->setState(gumballMachine->getHasQuarterState());
  134.         }
  135.          
  136.         virtual string printInfo() {
  137.             return "waiting for quarter";
  138.         }
  139.     };
  140.     
  141.     struct HasQuarterState : public State {
  142.      HasQuarterState(GumballMachine *gumballMachine) :
  143.             State(gumballMachine) {}
  144.     
  145.         virtual ejectQuarter() {
  146.             cout << "Quarter returned" << endl;
  147.             gumballMachine->setState(gumballMachine->getNoQuarterState());
  148.         }
  149.     
  150.         virtual turnCrank() {
  151.             cout << "You turned..." << endl;
  152.             gumballMachine->setState(gumballMachine->getSoldState());
  153.         }
  154.          
  155.         virtual string printInfo() {
  156.             return "waiting for turn of crank";
  157.         }
  158.     };
  159.     
  160.     struct SoldState : public State {
  161.      SoldState(GumballMachine *gumballMachine) :
  162.             State(gumballMachine) {}
  163.          
  164.         virtual dispense() {
  165.             gumballMachine->releaseBall();
  166.             if (gumballMachine->getCount() > 0) {
  167.                 gumballMachine->setState(gumballMachine->getNoQuarterState());
  168.             } else {
  169.                 cout << "Oops, out of gumballs!" << endl;
  170.                 gumballMachine->setState(gumballMachine->getSoldOutState());
  171.             }
  172.         }
  173.     
  174.         virtual string printInfo() {
  175.             return "dispensing a gumball";
  176.         }
  177.     };
  178.     
  179.     struct SoldOutState : public State {
  180.      SoldOutState(GumballMachine *gumballMachine) :
  181.             State(gumballMachine) {}
  182.     
  183.         virtual string printInfo() {
  184.             return "sold out";
  185.         }
  186.     };
  187. };

  188. int main(int argc, char **argv) {
  189.     GumballMachine *gumballMachine = new GumballMachine(5);

  190.     gumballMachine->printInfo();

  191.     gumballMachine->insertQuarter();
  192.     gumballMachine->turnCrank();
  193.     gumballMachine->printInfo();

  194.     gumballMachine->insertQuarter();
  195.     gumballMachine->turnCrank();

  196.     gumballMachine->insertQuarter();
  197.     gumballMachine->turnCrank();
  198.     gumballMachine->printInfo();
  199.     
  200.     gumballMachine->insertQuarter();
  201.     gumballMachine->turnCrank();
  202.     
  203.     gumballMachine->insertQuarter();
  204.     gumballMachine->turnCrank();
  205.     gumballMachine->printInfo();
  206. }

阅读(683) | 评论(0) | 转发(0) |
0

上一篇:迭代器模式

下一篇:代理模式

给主人留下些什么吧!~~