• 设计模式——状态模式(C++实现)


      1 /////////context.cpp
      2 #include "context.h"
      3 
      4 void STContext::ChangeState(STState* pstState)
      5 {
      6         m_pstState = pstState;
      7 }
      8 
      9 void STContext::request()
     10 {
     11         m_pstState->Handle(this);
     12 }
     13 /////////// context.h 
     14 #ifndef _CONTEXT_H_
     15 #define _CONTEXT_H_
     16 
     17 #include <iostream>
     18 #include <string>
     19 
     20 #include "state.h"
     21 
     22 using namespace std;
     23 
     24 
     25 class STState;
     26 
     27 class STContext
     28 {
     29 public:
     30         void ChangeState(STState* pstState);
     31         void request();
     32 
     33         STState* m_pstState;
     34 };
     35 
     36 #endif
     37 
     38 
     39 ///////////////////////////// state.cpp
     40 #include "state.h"
     41 
     42 void STConcreteStateA::Handle(STContext* pstContext)
     43 {
     44         cout<< "doing something in State A.
    done, change state to State B"<< endl;
     45         pstContext->ChangeState(new STConcreteStateB());
     46 }
     47 
     48 void STConcreteStateB::Handle(STContext* pstContext)
     49 {
     50         cout<< "doing something in State B.
    done, change state to State C"<< endl;
     51         pstContext->ChangeState(new STConcreteStateC());
     52 }
     53 
     54 void STConcreteStateC::Handle(STContext* pstContext)
     55 {
     56         cout<< "doing something in State C.
    done."<< endl;
     57 }
     58 
     59 /////////////////////////////// state.h
     60 #ifndef _STATE_H_
     61 #define _STATE_H_
     62 #include <iostream>
     63 #include <string>
     64 
     65 #include "context.h"
     66 
     67 using namespace std;
     68 
     69 class STContext;
     70 
     71 class STState
     72 {
     73 public:
     74         virtual void Handle(STContext*) = 0;
     75 };
     76 
     77 class STConcreteStateA: public STState
     78 {
     79 public:
     80         virtual void Handle(STContext* pstContext);
     81 };
     82 
     83 class STConcreteStateB: public STState
     84 {
     85 public:
     86         virtual void Handle(STContext* pstContext);
     87 };
     88 
     89 class STConcreteStateC: public STState
     90 {
     91 public:
     92         virtual void Handle(STContext* pstContext);
     93 };
     94 
     95 #endif
     96 
     97 
     98 ////////////////////// main.cpp
     99 #include <iostream>
    100 #include <string>
    101 
    102 #include "state.h"
    103 #include "context.h"
    104 
    105 using namespace std;
    106 
    107 int main(int argc, char* argv[])
    108 {
    109         STContext* pstContext = new STContext();
    110 
    111         STState* pstStateA = new STConcreteStateA();
    112 
    113         pstContext->ChangeState(pstStateA);
    114         pstContext->request();
    115         pstContext->request();
    116 
    117         return 0;
    118 }
    119 
    120 /////////////////////
    121 [wenchaozeng@devnet_10_10_tlinux_64 ~/learn_code/design_pattern/13_state]$ ./main
    122 doing something in State A.
    123 done, change state to State B
    124 doing something in State B.
    125 done, change state to State C
  • 相关阅读:
    机械学习--5
    机械学习--4
    机械学习--3
    机械学习--2
    机器学习--1
    编译原理 作业十五
    编译原理 作业十四
    编译原理 作业十二
    编译原理 作业十一
    编译原理 作业十
  • 原文地址:https://www.cnblogs.com/070412-zwc/p/6858766.html
Copyright © 2020-2023  润新知