zoukankan      html  css  js  c++  java
  • state模式

    http://www.cnblogs.com/graphicsme/archive/2011/12/09/2282657.html

    http://www.cnblogs.com/k-eckel/articles/204006.aspx

      1 #include <iostream>
      2 #include <string>
      3 #include <vector>
      4 #include <queue>
      5 #include <set>
      6 #include <algorithm>
      7 #include <map>
      8 #include <stack>
      9 using namespace std;
     10 class State;
     11 class Context {
     12 public:
     13     Context() {
     14     }
     15 
     16     Context(State* state);
     17 
     18     ~Context();
     19     void Handle();
     20 
     21     void OperationForStateA();
     22     void OperationForStateB();
     23     void ChangeState(State* state);
     24 
     25 private:
     26     State* _state;
     27 
     28 };
     29 
     30 class State {
     31 public:
     32     State() {
     33     }
     34 
     35     virtual ~State() {
     36     }
     37 
     38     virtual void Handle(Context* con) = 0;
     39 
     40 protected:
     41     void ChangeState(Context* con, State* st);
     42 
     43 };
     44 
     45 class ConcreteStateA: public State {
     46 public:
     47     ConcreteStateA() {
     48     }
     49 
     50     virtual ~ConcreteStateA() {
     51     }
     52 
     53     void Handle(Context* con);
     54 
     55 };
     56 
     57 class ConcreteStateB: public State {
     58 public:
     59     ConcreteStateB() {
     60     }
     61 
     62     virtual ~ConcreteStateB() {
     63     }
     64 
     65     void Handle(Context* con);
     66 
     67 };
     68 
     69 Context::Context(State* state) {
     70     this->_state = state;
     71 }
     72 
     73 Context::~Context() {
     74     delete _state;
     75 }
     76 
     77 void Context::Handle() {
     78     _state->Handle(this);
     79 }
     80 
     81 void Context::ChangeState(State* state) {
     82     this->_state = state;
     83 
     84 }
     85 
     86 void Context::OperationForStateA() {
     87     cout << "Do operation in State A ";
     88 }
     89 
     90 void Context::OperationForStateB() {
     91     cout << "Do operation in State B ";
     92 }
     93 
     94 void State::ChangeState(Context* con, State* st) {
     95     con->ChangeState(st);
     96 }
     97 
     98 void ConcreteStateA::Handle(Context* con) {
     99     con->OperationForStateA();
    100 
    101     cout << ":: State change from A to B" << endl;
    102 
    103     State::ChangeState(con, new ConcreteStateB());
    104 }
    105 
    106 void ConcreteStateB::Handle(Context* con) {
    107     con->OperationForStateB();
    108 
    109     cout << ":: State change from B to A" << endl;
    110 
    111     State::ChangeState(con, new ConcreteStateA());
    112 }
    113 
    114 int main(int argc, char* argv[]) {
    115     State* st = new ConcreteStateA();
    116 
    117     Context* con = new Context(st);
    118 
    119     con->Handle();
    120     con->Handle();
    121     con->Handle();
    122 
    123     if (con != NULL)
    124         delete con;
    125 
    126     if (st != NULL)
    127         st = NULL;
    128 
    129     return 0;
    130 }
  • 相关阅读:
    requirejs 第一个实例
    ionic + cordova 环境搭建
    免安装mysql配置
    ConcurrentHashMap
    volatile和synchronized
    zookeeper集群安装
    题目
    Nginx
    CountDownLatch
    自己总结
  • 原文地址:https://www.cnblogs.com/kakamilan/p/2610751.html
Copyright © 2011-2022 走看看