当前位置:编程学习 > C/C++ >>

State模式

意图:允许一个对象在其内部状态改变时改变它的行为;对象看起来似乎修改了它的类;在一个状态即将结束的时候启用下一个状态,可以是一个按需的连锁反应;

适用性:

1、一个对象的行为取决于它的状态,并且它必须在运行过程中可以根据它的状态改变它的行为;

2、一个操作中含有庞大的多分支的条件语句,并且这些分支依赖于该对象的状态;这个状态通常使用一个或多个枚举常量表示;通常,有多个操作包含这一相同的条件结构;State模式把每一个条件分支放入一个独立的类中;这使得你可以根据对象自身的情况把对象的状态作为一个对象,而且这一对象可以不依赖于其它对象而独立变化;

结构图:

 

[cpp]
应用: 
#ifndef _STATE_H_ 
#define _STATE_H_ 
 
#include <iostream> 
#include <string> 
 
using namespace std; 
 
class TCPStream; 
 
//需要被改变状态的对象类;它聚合了一个状态对象:TCPState;依据TCPState对象的变化来改变TCPConnection对象的行为; 
class TCPConnection 

  public: 
    TCPConnection(void); 
    ~TCPConnection(void); 
 
    void ActiveOpen(void); 
    void PassiveOpen(void); 
    void Close(void); 
    void Send(void); 
    void Acknowledge(void); 
    void Synchronize(void); 
    void ProcessStream(TCPStream*); 
 
  private: 
    friend class TCPState; 
    void ChangeState(TCPState*); 
 
  private: 
    TCPState* m_lpState; 
}; 
 
//TCPConnection类对象的状态对象:TCPState;它实现为一个纯虚基类; 
class TCPState 

  public: 
    TCPState(void); 
    virtual ~TCPState(void); 
    virtual void Transmit(TCPConnection*, TCPStream*); 
    virtual void ActiveOpen(TCPConnection*); 
    virtual void PassiveOpen(TCPConnection*); 
    virtual void Close(TCPConnection*); 
    virtual void Synchronize(TCPConnection*); 
    virtual void Acknowledge(TCPConnection*); 
    virtual void Send(TCPConnection*); 
  protected: 
    void ChangeState(TCPConnection*, TCPState*); 
}; 
 
//连接已建立状态 
class TCPEstablished: public TCPState 

  public: 
    virtual ~TCPEstablished(void); 
    static TCPState* Instance(void); 
    virtual void Transmit(TCPConnection*, TCPStream*); 
    virtual void Close(TCPConnection*); 
 
  private: 
    TCPEstablished(void); 
}; 
 
//连接关闭状态 
class TCPClosed: public TCPState 

  public: 
    virtual ~TCPClosed(void); 
    static TCPState* Instance(void); 
    virtual void ActiveOpen(TCPConnection*); 
    virtual void PassiveOpen(TCPConnection*); 
  private: 
    TCPClosed(void); 
}; 
 
//监听状态类 
class TCPListen: public TCPState 

  public: 
    ~TCPListen(void); 
    static TCPState* Instance(void); 
    virtual void Send(TCPConnection*); 
  private: 
    TCPListen(void); 
}; 
 
TCPConnection::TCPConnection(void) 

  this->m_lpState = TCPClosed::Instance(); 
  cout << "call TCPConnection()" << endl; 

 
TCPConnection::~TCPConnection(void) 

  cout << "call ~ TCPConnection()" << endl; 

 
void TCPConnection::ActiveOpen(void) 

  this->m_lpState->ActiveOpen(this); 
  cout << "TCPConnection active-open state" << endl; 

 
void TCPConnection::PassiveOpen(void) 

  this->m_lpState->PassiveOpen(this); 
  cout << "TCPConnection passive-open state" << endl; 

 
void TCPConnection::Close(void) 

  this->m_lpState->Close(this); 
  cout << "TCPConnection close state" << endl; 

 
void TCPConnection::Send(void) 

  this->m_lpState->Send(this); 
  cout << "TCPConnection send state" << endl; 

 
void TCPConnection::Acknowledge(void) 

  this->m_lpState->Acknowledge(this); 
  cout << "TCPConnection ack state" << endl; 

 
void TCPConnection::Synchronize(void) 

  this->m_lpState->Synchronize(this); 
  cout << "TCPConnection sync state" << endl; 

 
void TCPConnection::ProcessStream(TCPStream* st) 

  cout << "TCPConnection ProcessStream" << endl; 

 
void TCPConnection::ChangeState(TCPState* state) 

  if(this->m_lpState) 
  { 
    delete this->m_lpState; 
    this->m_lpState = NULL; 
  } 
  this->m_lpState = state; 
  cout << "TCPConnection::ChangeState()" << endl; 

 
TCPState::TCPState(void) 

  cout << "call TCPState()" << endl; 

 
TCPState::~TCPState(void) 

  cout << "call ~ TCPState()" << endl; 

 
void TCPState::Transmit(TCPConnection*, TCPStream*) 

  cout << "TCPState::Transmit()" << endl;  

 
void TCPState::ActiveOpen(TCPConnection*) 

  cout << "TCPState::ActiveOpen()" << endl;  

 
void TCPState::PassiveOpen(TCPConnection*) 

  cout << "TCPState::PassiveOpen()" << endl;  

 
void TCPState::Close(TCPConnection*) 

  cout << "TCPState::Close()" << endl;  

 
void TCPState::Synchronize(TCPConnection*) 

  cout << "TCPState::Synchronize()" << endl;  

 
void TCPState::Acknowledge(TCPConnection*) 

  cout << "TCPState::Acknowledge()" << endl;&nbs

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,