意图:
允许一个对象在其内部状态改变时改变他的行为
适用性:
1一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为
2一个操作中含有庞大的多分支的条件语句,且这些分支依赖于该对象的状态。这种模式将每一个条件分支放入一个独立的类中,根据对象自身的状态将对象的状态作为一个对象
效果:
1它将与特定状态相关的行为局部化,并且将不同状态的行为分割开来
2 它使得状态显式化,从Context角度来看,状态转换是原子的,只需重新邦定一个变量即可
3 State对象可被共享
实现:
1谁定义状态转换 State模式不指定哪一个参与者定义状态转换规则,如果是固定的,则可以在Context中完全实现
2创建和销毁State对象
当将要进入的状态在运行时是不可知的,并且上下文不经常改变状态时,仅当需要State对象时才创建他们并随后销毁它们
当状态改变很频繁时,采用提前创建他们并始终不销毁它们的方法
The sample code is as below:
State.h
- #include <iostream>
- using namespace std;
- class TCPState;
- class TCPConnection
- {
- public:
- TCPConnection();
- void ActiveOpen();
- void PassiveOpen();
- void Close();
- void Send();
- //void Acknowledge();
- //void Synchronize();
- void ChangeState(TCPState*);
- protected:
- friend class TCPstate;
- private:
- TCPState* _state;
- };
- class TCPState
- {
- public:
- virtual void Transmit(TCPConnection*);
- 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:
- static TCPState* Instance();
- // virtual void Transmit(TCPConnection*);
- virtual void Close(TCPConnection*);
- private:
- static TCPState* _instance;
- };
- class TCPListen : public TCPState
- {
- public:
- static TCPState* Instance();
- //virtual void Send(TCPConnection*);
- private:
- static TCPState* _instance;
- };
- class TCPClosed :public TCPState
- {
- public:
- TCPClosed() {cout<<"TCPClosed is created"<<endl;}
- static TCPState* Instance();
- virtual void ActiveOpen(TCPConnection*);
- virtual void PassiveOpen(TCPConnection*);
- private:
- static TCPState* _instance;
- };
State.cpp
- #include "State.h"
- #include <iostream>
- using namespace std;
- TCPState* TCPClosed::_instance =0;
- TCPState* TCPListen::_instance=0;
- TCPState* TCPEstablished::_instance=0;
- TCPState* TCPClosed::Instance()
- {
- if (_instance == 0)
- {
- _instance = new TCPClosed;
- }
- cout<<"Change the state to TCPClosed"<<endl;
- return _instance;
- }
- TCPState* TCPListen::Instance()
- {
- if (_instance == 0)
- {
- _instance = new TCPListen;
- }
- cout<<"Change the state to TCPListen"<<endl;
- return _instance;
- }
- TCPState* TCPEstablished::Instance()
- {
- if (_instance == 0)
- {
- _instance = new TCPEstablished;
- }
- cout<<"Change the state to TCPEstablished"<<endl;
- return _instance;
- }
- 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::Send()
- {
- _state->Transmit(this);
- }
- void TCPState::Transmit(TCPConnection*)
- {
- cout<<"TCP in Transmit state"<<endl;
- }
- void TCPState::ActiveOpen(TCPConnection*)
- {
- cout<<"TCP in Active open state"<<endl;
- }
- void TCPState::PassiveOpen(TCPConnection*)
- {
- cout<<"TCP in passive open state"<<endl;
- }
- void TCPState::Close(TCPConnection*)
- {
- cout<<"TCP in close state"<<endl;
- }
- void TCPState::ChangeState(TCPConnection* t,TCPState* s)
- {
- t->ChangeState(s);
- }
- void TCPClosed::ActiveOpen(TCPConnection* t)
- {
- cout<<"TCPClosed send SYN,receive SYN,ACK"<<endl;
- ChangeState(t,TCPEstablished::Instance());
- }
- void TCPClosed::PassiveOpen(TCPConnection* t)
- {
- ChangeState(t,TCPEstablished::Instance());
- }
- void TCPEstablished::Close(TCPConnection* t)
- {
- cout<<"TCPEstablished send FIN,receive ACK of FIN"<<endl;
- ChangeState(t,TCPEstablished::Instance());
- }
main.cpp
- #include "State.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- TCPConnection a;
- a.ActiveOpen();
- a.Close();
- return 0;
- }