C语言实现状态设计模式
2016-04-26 10:36
387 查看
C语言实现状态设计模式
c(20)![](http://static.blog.csdn.net/images/arrow_triangle%20_down.jpg)
![](http://static.blog.csdn.net/images/arrow_triangle_up.jpg)
作者同类文章X
版权声明:本文为博主原创文章,未经博主允许不得转载。
#include <stdio.h> #include <stdlib.h> #include <string.h> #ifndef VIRTUAL #define VIRTUAL #endif #ifndef DELETE #define DELETE(X) do { free(X);X = NULL; } while(0) #endif
//宏定义:新建一个以TYPE为父类型的SUBTYPE子类型的实例,实例名为SUBTYPE##instance,首先是分配内存,然后指定实例的实际处理函数(类似于函数委托)
//通过该宏定义可以实例化从同一基类派生的不同子类型,并且可以动态的指定子类型的构造、实例化 #define NEW(TYPE,pInstance,SUBTYPE) struct TYPE* pInstance = NULL; \ struct SUBTYPE* SUBTYPE##instance = (struct SUBTYPE*)malloc(sizeof(struct SUBTYPE)); \ SUBTYPE##instance->SUBTYPE##_constructor = SUBTYPE##_constructor; \ SUBTYPE##instance->SUBTYPE##_constructor(SUBTYPE##instance);pInstance = (struct TYPE*)(SUBTYPE##instance); struct State* getProphaseState(void); //获得战争前期的状态 struct State* getMetaphaseState(void);//获得战争中期的状态 struct State* getAnaphaseState(void);//获得战争后期的状态 struct State* getEndState(void); //获得战争结束的状态 struct War { struct State* m_pState; //战争当前的状态 int m_nDays; //战争进行的天数 void (*War_constructor)(struct War* /* pThis */); void (*War_destructor)(struct War* /* pThis */); int (*War_getDays)(struct War* /* pThis */); void (*War_setDays)(struct War* /* pThis */,int /* nDays */); void (*War_setState)(struct War* /* pThis */,struct State* /* pState */); void (*War_exeState)(struct War* /* pThis */); };
//战争状态基类 struct State /* base class */ { VIRTUAL void (*Action)(struct State* /* pThis */,struct War* /* pWar */); //函数指针,对每种战争状态的处理 }; struct EndState /* sub class */ { struct State m_nParent; //C中以此实现继承 void (*EndState_constructor)(struct EndState* /* pThis */); //指定m_nParent的Action指向(根据之前宏定义指向EndState_constructor函数) }; struct AnaphaseState /* sub class */ { struct State m_nParent; void (*AnaphaseState_constructor)(struct AnaphaseState* /* pThis */); }; struct MetaphaseState /* sub class */ { struct State m_nParent; void (*MetaphaseState_constructor)(struct MetaphaseState* /* pThis */); }; struct ProphaseState { struct State m_nParent; void (*ProphaseState_constructor)(struct ProphaseState* /* pThis */); }; void EndState_Action(struct State* pThis,struct War* pWar) { printf("444\n"); } void AnaphaseState_Action(struct State* pThis,struct War* pWar) { if(pWar->War_getDays(pWar) < 30) { printf("333 : %u\n",pWar->War_getDays(pWar)); } else { pWar->War_setState(pWar,getEndState()); pWar->War_exeState(pWar); } } void MetaphaseState_Action(struct State* pThis,struct War* pWar) { if(pWar->War_getDays(pWar) < 20) { printf("222 : %u\n",pWar->War_getDays(pWar)); } else { pWar->War_setState(pWar,getAnaphaseState()); pWar->War_exeState(pWar); } } void ProphaseState_Action(struct State* pThis,struct War* pWar) { if(pWar->War_getDays(pWar) < 10) { printf("111 : %u\n",pWar->War_getDays(pWar)); } else { pWar->War_setState(pWar,getMetaphaseState()); pWar->War_exeState(pWar); } } void EndState_constructor(struct EndState* pThis) { pThis->m_nParent.Action = EndState_Action; } void AnaphaseState_constructor(struct AnaphaseState* pThis) { pThis->m_nParent.Action = AnaphaseState_Action; } void MetaphaseState_constructor(struct MetaphaseState* pThis) { pThis->m_nParent.Action = MetaphaseState_Action; } void ProphaseState_constructor(struct ProphaseState* pThis) { pThis->m_nParent.Action = ProphaseState_Action; } /* functions releated with the class War */ void War_destructor(struct War* pThis) { if(pThis->m_pState) { DELETE(pThis->m_pState); } } int War_getDays(struct War* pThis) { return pThis->m_nDays; } void War_setDays(struct War* pThis,int nDays) { pThis->m_nDays = nDays; } void War_setState(struct War* pThis,struct State* pState) { if(pThis->m_pState) { printf("delete %p\n",pThis->m_pState); DELETE(pThis->m_pState); } pThis->m_pState = pState; } void War_exeState(struct War* pThis) { pThis->m_pState->Action(pThis->m_pState,pThis); } void War_constructor(struct War* pThis) { pThis->m_pState = NULL; pThis->m_nDays = 0; pThis->War_destructor = War_destructor; pThis->War_getDays = War_getDays; pThis->War_setDays = War_setDays; pThis->War_setState = War_setState; pThis->War_exeState = War_exeState; } /* get instance */ struct State* getProphaseState(void) { NEW(State,pState,ProphaseState); printf("new %p\n",pState); return pState; } struct State* getMetaphaseState(void) { NEW(State,pState,MetaphaseState); printf("new %p\n",pState); return pState; } struct State* getAnaphaseState(void) { NEW(State,pState,AnaphaseState); printf("new %p\n",pState); return pState; } struct State* getEndState(void) { NEW(State,pState,EndState); printf("new %p\n",pState); return pState; } /* test */ int main(void) { NEW(War,pWar,War); printf("init\n"); pWar->War_setDays(pWar,0); pWar->War_setState(pWar,getProphaseState()); for(int i = 1 ; i < 40 ; i += 1) { pWar->War_setDays(pWar,i); pWar->War_exeState(pWar); } pWar->War_destructor(pWar); DELETE(pWar); system("pause"); return 0; }
相关文章推荐
- Keil uVision编译支持C++
- C++11新特性
- printf标准输出,右对齐
- C/C++字符串和数字互换方案收集(转)
- c++中vector的用法详解
- 如何优化C语言代码(程序员必读)
- 数组实现队列 C++实现(2)
- 关于c++常量定义
- c/c++ 数字转成字符串, 字符串转成数字
- C++中产生随机数
- C++中 public protected private 类成员的访问
- 关于优化C++的小知识
- 关于C++中的常量成员函数
- C++中string类对象的成员函数c_str()与C语言中字符串的联系
- C++读取配置文件
- C语言之程序编程规范
- c语言 统计数组中不同字符的个数
- 【运动控制】C++接口要求
- 值得推荐的C/C++框架和库
- Effective C++ 55个条款