//
// TcpState.h
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#ifndef TcpState_h
#define TcpState_h
#include <iostream>
#include <string>
class TcpConnection;
//#include "TcpConnection.h"
using namespace std;
class TcpState
{
public:
TcpState(){
cout<<"TcpState初始化"<<endl;
cout<<instance<<endl;
}
virtual ~TcpState(){}
/*把子類的所有行爲封裝到接口中,每個函數都有一個TcpConnection作爲參數從而訪問TcpConnection中的數據和改變連接的狀態*/
virtual void transmit(TcpConnection* con,string message){
cout<<message<<endl;}
virtual void activeOpen(TcpConnection* con){}
virtual void passiveOpen(TcpConnection* con){}
virtual void close(TcpConnection* con){}
virtual void syschronize(TcpConnection* con){}
virtual void acknowledge(TcpConnection* con){}
virtual void send(TcpConnection* con){}
static void release(){ std::cout<<"TcpState"<<endl;}
protected:
/*可有可無,其本質就是調用Context方法改變其狀態,可再其方法中實現*/
void changeState(TcpConnection* con, TcpState* state);
static TcpState* instance;
};
#endif /* TcpState_h */
//
// TcpState.cpp
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#include "TcpState.h"
#include "TcpConnection.h"
TcpState* TcpState::instance = 0;
void TcpState::changeState(TcpConnection *con, TcpState *state)
{
con->changeState(state);
}
//
// TcpListen.h
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#ifndef TcpListen_h
#define TcpListen_h
#include <iostream>
#include "TcpState.h"
class TcpListen:public TcpState
{
public:
static TcpListen* getInstace()
{
if (instance == nullptr) {
return new TcpListen;
}
return dynamic_cast<TcpListen*>(instance);
}
static void release()
{
if (instance != nullptr) {
delete instance;
instance = nullptr;
}
}
virtual void send(TcpConnection *con);
private:
TcpListen(){}
TcpListen(const TcpListen& );
TcpListen& operator= (const TcpListen&);
virtual ~TcpListen(){}
//static TcpState* instance;
};
#endif /* TcpListen_h */
//
// TcpListen.cpp
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#include "TcpListen.h"
#include "TcpEstablished.h"
using namespace std;
//TcpState* TcpListen::instance = 0;
void TcpListen::send(TcpConnection *con)
{
cout<<"TcpListen::send"<<endl;
changeState(con, TcpEstablished::getInstance());
}
//
// TcpEstablished.h
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#ifndef TcpEstablished_h
#define TcpEstablished_h
#include <iostream>
#include "TcpState.h"
class TcpEstablished:public TcpState
{
public:
/*單例設計模式*/
static TcpEstablished* getInstance(){
if (instance == nullptr) {
return new TcpEstablished;
}
return dynamic_cast<TcpEstablished*>(instance);
}
static void release(){
if (instance != nullptr) {
delete instance;
instance = nullptr;
}
}
virtual void transmit(TcpConnection *con, string message);
virtual void close(TcpConnection *con);
private:
TcpEstablished(){}
virtual ~TcpEstablished(){}
TcpEstablished(const TcpEstablished& );
TcpEstablished& operator=(const TcpEstablished& );
//static TcpState* instance;
};
#endif /* TcpEstablished_h */
//
// TcpEstablished.cpp
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#include "TcpEstablished.h"
#include "TcpListen.h"
using namespace std;
//TcpState* TcpEstablished::instance = 0;
void TcpEstablished::transmit(TcpConnection *con, string message)
{
cout<<"TcpEstablished::transmit"<<message<<endl;
changeState(con, TcpListen::getInstace());
}
void TcpEstablished::close(TcpConnection *con)
{
cout<<"TcpEstablished::close"<<endl;
/*具體定義轉換成哪種狀態,調用TcpConnection類的changeState統一執行*/
changeState(con, TcpListen::getInstace());
}
//
// TcpClosed.h
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#ifndef TcpClosed_h
#define TcpClosed_h
#include <iostream>
#include "TcpState.h"
class TcpClosed:public TcpState
{
public:
static TcpClosed* getInstance(){
if (instance == nullptr) {
return new TcpClosed;
}
return dynamic_cast<TcpClosed*>(instance);
}
static void release(){
if (instance != nullptr) {
delete instance;
instance = nullptr;
}
}
virtual void activeOpen(TcpConnection *con);
virtual void passiveOpen(TcpConnection* con);
private:
TcpClosed(){}
virtual ~TcpClosed(){}
TcpClosed(const TcpClosed&);
TcpClosed& operator= (const TcpClosed&);
};
#endif /* TcpClosed_h */
//
// TcpClosed.cpp
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#include "TcpClosed.h"
#include "TcpEstablished.h"
#include "TcpListen.h"
using namespace std;
void TcpClosed::activeOpen(TcpConnection *con)
{
cout<<"TcpClosed::activeOpen"<<endl;
changeState(con, TcpEstablished::getInstance());
}
void TcpClosed::passiveOpen(TcpConnection *con)
{
cout<<"TcpClosed::passiveOpen"<<endl;
changeState(con, TcpListen::getInstace());
}
//
// TcpConnection.h
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#ifndef TcpConnection_h
#define TcpConnection_h
#include <iostream>
#include "TcpState.h"
#include "TcpListen.h"
#include "TcpEstablished.h"
#include "TcpClosed.h"
using namespace std;
class TcpConnection
{
public:
TcpConnection(){ state = TcpClosed::getInstance(); }
virtual ~TcpConnection(){ /*state->release();*/}
void activeOpen(){ state->activeOpen(this); }
void passiveOpen(){ state->passiveOpen(this); }
void close(){ state->close(this); }
void send(){ state->send(this); }
void acknowledge(){ state->acknowledge(this); }
void synchronize(){ state->syschronize(this); }
void transmit(string message){ state->transmit(this, message); }
private:
/*必須要有changeState方法供狀態類來調用改變狀態*/
void changeState(TcpState *state){this->state = state;}
friend class TcpState;
private:
TcpState* state;
};
#endif /* TcpConnection_h */
//
// TcpConnection.cpp
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#include "TcpConnection.h"
客戶端代碼:
//
// main.cpp
// textD
//
// Created by md101 on 15/10/19.
// Copyright © 2015年 md101. All rights reserved.
//
#include <iostream>
#include "TcpConnection.h"
using namespace std;
int main(int argc, const char * argv[]) {
TcpConnection *con = new TcpConnection();
con->activeOpen();
con->transmit("xgdsgdfsg");
con->send();
TcpClosed::release();
TcpEstablished::release();
TcpListen::release();
delete con;
return 0;
}