设计模式 Design Parttern ——状态模式State http://blog.youkuaiyun.com/leeidea //1:头文件 #ifndef _STATE_H_VANEY_ #define _STATE_H_VANEY_ #include <iostream> using namespace std; /****************************************************************** 名称 :State.h 版本 :1.00 描述 :演示状态模式的概念 作者 :vaney.li@gmail.com http://blog.youkuaiyun.com/leeidea 日期 :2010年10月28日 版权 :vaney.li@gmail.com http://blog.youkuaiyun.com/leeide ******************************************************************/ /* 官方解释:The State pattern allows an object to change its behavior when its internal state changes. 我的理解:一个事物由很多因素决定事物的状态,各种因素的集合代表一个状态类,不同的状态类将改变 事务的行为。 我的应用: */ //抽象访问者 class CNetProtocol { public: CNetProtocol(char* str) { if(str) { int len = strlen(str); m_str = (char*)malloc(len + 1); memcpy(m_str,str,len); m_str[len] = 0x00; } cout << "CNetProtocol()" << endl; } virtual ~CNetProtocol() { if(m_str) free(m_str); m_str = 0; cout << "~CNetProtocol()" << endl; } public: virtual char* GetHead() { return m_str; } protected: char* m_str; }; //抽象对象 class CHTTP : public CNetProtocol { public: CHTTP():CNetProtocol("HTTP 1.1") { cout << "CHTTP()" << endl; } virtual ~CHTTP() { cout << "~CHTTP()" << endl; } }; class CTCP : public CNetProtocol { public: CTCP():CNetProtocol("TCP") { cout << "CTCP()" << endl; } virtual ~CTCP() { cout << "~CTCP()" << endl; } }; class CState { public: CState(CNetProtocol* protocol):m_protocol(protocol) { cout << "CState()" << endl; } virtual ~CState() { if(m_protocol) delete m_protocol; m_protocol = 0; cout << "~CState()" << endl; } protected: CNetProtocol* m_protocol; virtual int IsHttpOrTcpData() = 0; virtual void Parser() = 0; }; class CParserHttp : public CState { public: CParserHttp(CNetProtocol* protocol):CState(protocol) { cout << "CParserHttp()" << endl; } virtual ~CParserHttp() { cout << "~CParserHttp()" << endl; } public: virtual int IsHttpOrTcpData() { if(m_protocol) { if(strstr(m_protocol->GetHead(),"HTTP 1.1")) { cout << "IsHttp" << endl; return 1; } return 0; } return 0; } virtual void Parser() { cout << "Parser Http" << endl; } }; class CParserTcp : public CState { public: CParserTcp(CNetProtocol* protocol):CState(protocol) { cout << "CParserTcp()" << endl; } virtual ~CParserTcp() { cout << "~CParserTcp()" << endl; } public: virtual int IsHttpOrTcpData() { if(m_protocol) { if(strstr(m_protocol->GetHead(),"TCP")) { cout << "IsTcpData" << endl; return 1; } return 0; } return 0; } virtual void Parser() { cout << "Parser Tcp" << endl; } }; #define C_API extern "C" //用户 C_API int UsingST(); #endif //2:源文件 #include "State.h" C_API int UsingST() { //数据流可以是从网络上获取的,可能对方有很多种网络流格式,客户端 //开始不知道是哪种,但客户端需要做到最大化的支持各种各样的流。 char* str = "HTTP 1.1"; CNetProtocol* protocol = new CNetProtocol(str); CParserHttp* parser_http = new CParserHttp(protocol); CParserTcp* parser_tcp = new CParserTcp(protocol); if(parser_http->IsHttpOrTcpData()) { parser_http->Parser(); } else if(parser_tcp->IsHttpOrTcpData()) { parser_tcp->Parser(); } return 1; } //3:用户文件main.c extern int UsingST(); //系统默认入口 int _tmain(int argc, _TCHAR* argv[]) { return UsingST(); }