意图:
允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。
别名:
状态对象(Objects for States)
3、动机
考虑一个表示网络连接的类TCPConnection。一个TCPConnection对象的状态处于若干不同状态之一:
连接已建立(Established),正在监听(Listening),连接已关闭(Closed)。当一个TCPConnection对象
收到其他对象的请求时,它根据自身的当前状态作出不同的反应。例如,一个Open请求结果依赖于该
连接是处于连接已关闭状态还是连接已建立状态。State模式描述了TCPConnection如何在每一种状态
下表现出不同的行为。
这一模式的关键思想是引入了一个称为TCPState的抽象类来表示网络的连接状态。TCPState类为各表示
不同的操作状态的子类声明了一个公共接口。TCPState的子类实现与特定状态相关的行为。例如TCPEstablished
和TCPClosed类分别实现了特定于TCPConnection的连接已建立状态和连接已关闭状态的行为。
TCPConnection类维护一个表示TCP连接当前状态的状态对象(一个TCPState子类的实例)。TCPConnection类
将所有与状态相关的请求委托给这个状态对象。TCPConnection使用它的TCPState子类实例来执行特定于连接
状态的操作。
一旦连接状态改变,TCPConnection对象就会改变它所使用的状态对象。例如当连接从已建立状态转为已关闭
状态时,TCPConnection会用一个TCPClosed的实例来代替原来的TCPEstablished的实例。
4、适用性
在下面的两种情况下均可使用State模式:
一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为。
一个操作中含有庞大的多分支的条件语句,且这些分支依赖于该对象的状态。(State模式将每一个条件分支放入一个独立
的类中。这使得你可以根据对象自身的情况将对象的状态作为一个对象,这一对象可以不依赖于其它对象而独立变化。
5、效果:
它将与特定状态相关的行为局部化,并且将不同状态的行为分割开来。
它使得状态转换显示化。
State对象可被共享。
代码示例:

//
state.h: interface for the state class.
//

/**/
//
#pragma
once


/**/
/*
*/


/**/
/*
*/
class
TCPOctetStream;
class
TCPState;


class
TCPConnection
...
{
public:
TCPConnection();

/**//*
*/
void ActiveOpen();
void PassiveOpen();
void Close();

/**//*
*/
void Send();
void Acknowledge();
void Synchronize();

/**//*
*/

void ProcessOctet(TCPOctetStream*)...{}
private:
friend class TCPState;
void ChangeState(TCPState*);
private:
TCPState* _state;
}
;

/**/
/*
*/

class
TCPState
...
{
public:
virtual void Transmit(TCPConnection*, TCPOctetStream*);
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
TCPClosed :
public
TCPState
...
{
public:
static TCPState* Instance();

virtual void ActiveOpen(TCPConnection*);
virtual void PassiveOpen(TCPConnection*);
// ...
private:
static TCPState* _instance;

}
;

/**/
/*
*/



class
TCPEstablished :
public
TCPState
...
{
public:
static TCPState* Instance();

virtual void Transmit(TCPConnection*, TCPOctetStream*);
virtual void Close(TCPConnection*);
private:
static TCPState* _instance;

}
;

/**/
/*
*/

class
TCPListen :
public
TCPState
...
{
public:
static TCPState* Instance();

virtual void Send(TCPConnection*);
// ...
private:
static TCPState* _instance;

}
;




//
state.cpp: implementation of the state class.
//

/**/
//
#include
"
stdafx.h
"
#include
"
state.h
"


/**/
//
//
Construction/Destruction

/**/
//
TCPState
*
TCPClosed::_instance
=
NULL;
TCPState
*
TCPEstablished::_instance
=
NULL;
TCPState
*
TCPListen::_instance
=
NULL;

TCPConnection::TCPConnection ()
...
{
_state = TCPClosed::Instance();
}

/**/
/*
*/

void
TCPConnection::ChangeState (TCPState
*
s)
...
{
_state = s;
}

/**/
/*
*/

void
TCPConnection::ActiveOpen ()
...
{
_state->ActiveOpen(this);
}

/**/
/*
*/

void
TCPConnection::PassiveOpen ()
...
{
_state->PassiveOpen(this);
}

/**/
/*
*/

void
TCPConnection::Close ()
...
{
_state->Close(this);
}


/**/
/*
*/

void
TCPConnection::Acknowledge ()
...
{
_state->Acknowledge(this);
}

/**/
/*
*/

void
TCPConnection::Synchronize ()
...
{
_state->Synchronize(this);
}

/**/
/*
*/

void
TCPState::Transmit (TCPConnection
*
, TCPOctetStream
*
)
...
{ }

void
TCPState::ActiveOpen (TCPConnection
*
)
...
{ }

void
TCPState::PassiveOpen (TCPConnection
*
)
...
{ }

void
TCPState::Close (TCPConnection
*
)
...
{ }

void
TCPState::Synchronize (TCPConnection
*
)
...
{ }

/**/
/*
*/

void
TCPState::ChangeState (TCPConnection
*
t, TCPState
*
s)
...
{
t->ChangeState(s);
}

/**/
/*
*/


TCPState
*
TCPClosed::Instance()

...
{
if(_instance == NULL)

...{
_instance = new TCPClosed;
}
return _instance;
}

void
TCPClosed::ActiveOpen (TCPConnection
*
t)
...
{
// send SYN, receive SYN, ACK, etc.

ChangeState(t, TCPEstablished::Instance());
}

/**/
/*
*/

void
TCPClosed::PassiveOpen (TCPConnection
*
t)
...
{
ChangeState(t, TCPListen::Instance());
}

/**/
/*
*/

TCPState
*
TCPEstablished::Instance()

...
{
if(_instance == NULL)

...{
_instance = new TCPEstablished;
}
return _instance;
}

void
TCPEstablished::Close (TCPConnection
*
t)
...
{
// send FIN, receive ACK of FIN

ChangeState(t, TCPListen::Instance());
}

/**/
/*
*/
void
TCPEstablished::Transmit (
TCPConnection
*
t, TCPOctetStream
*
o

)
...
{
t->ProcessOctet(o);
}

/**/
/*
*/

TCPState
*
TCPListen::Instance()

...
{
if(_instance == NULL)

...{
_instance = new TCPListen;
}
return _instance;
}

void
TCPListen::Send (TCPConnection
*
t)
...
{
// send SYN, receive SYN, ACK, etc.

ChangeState(t, TCPEstablished::Instance());
}

/**/
/*
*/