设计模式——状态模式(State)

  • 定义:允许一个对象在其状态改变时,改变它的行为。看起来对象似乎修改了它的类。
  • 解决根据不同的对象状态进行不同的操作的问题
  • UML

  • 代码示例:
      1 //==========================================
      2 //Context.h
      3 //==========================================
      4 #pragma once
      5 
      6 class State;
      7 class Context
      8 {
      9 public:
     10     Context(State* pState);
     11     ~Context();
     12     void Request();
     13 private:
     14     void UpdateState(State* pState);
     15     friend class ConcreteStateA;
     16     friend class ConcreteStateB;
     17 private:
     18     State* m_pState;
     19 };
     20 
     21 //==========================================
     22 //Context.cpp
     23 //==========================================
     24 #include "Context.h"
     25 #include <stdio.h>
     26 #include "State.h"
     27 
     28 Context::Context(State* pState)
     29     :m_pState(pState)
     30 {
     31 }
     32 
     33 Context::~Context()
     34 {
     35     delete m_pState;
     36 }
     37 
     38 void Context::Request()
     39 {
     40     if (NULL != m_pState)
     41     {
     42         m_pState->Handle(this);
     43     }
     44 }
     45 
     46 void Context::UpdateState(State* pState)
     47 {
     48     delete m_pState;
     49     m_pState = pState;
     50 }
     51 
     52 //==========================================
     53 //State.h
     54 //==========================================
     55 #pragma once
     56 
     57 class Context;
     58 class State
     59 {
     60 public:
     61     virtual ~State(){}
     62     virtual void Handle(Context* pContex) = 0;
     63 };
     64 
     65 class ConcreteStateA : public State
     66 {
     67 public:
     68     void Handle(Context* pContex);
     69 };
     70 
     71 class ConcreteStateB : public State
     72 {
     73 public:
     74     void Handle(Context* pContex);
     75 };
     76 
     77 //==========================================
     78 //State.cpp
     79 //==========================================
     80 #include "State.h"
     81 #include <iostream>
     82 #include "Context.h"
     83 using namespace std;
     84 
     85 //处理、更新状态
     86 void ConcreteStateA::Handle(Context* pContex)
     87 {
     88     cout<<"ConcreteStateA :: Handle"<<endl;
     89     pContex->UpdateState(new ConcreteStateB);
     90 }
     91 
     92 void ConcreteStateB::Handle(Context* pContex)
     93 {
     94     cout<<"ConcreteStateB :: Handle"<<endl;
     95     pContex->UpdateState(new ConcreteStateA);
     96 }
     97 
     98 //==========================================
     99 //main.cpp
    100 //==========================================
    101 #include "Context.h"
    102 #include "State.h"
    103 
    104 int main()
    105 {
    106     State *pState = new ConcreteStateA();
    107     Context *pContext = new Context(pState);
    108     pContext->Request();
    109     pContext->Request();
    110     pContext->Request();
    111 
    112     delete pContext;
    113 
    114     return 0;
    115 }
posted @ 2013-01-04 11:18  iThinking  阅读(256)  评论(0编辑  收藏  举报