C语言实现《大话设计模式》中的状态模式例程#ifndef __WORK_H__#define __WORK_H__#include "rtthread.h"#include "State.h"//工作类typedef struct _State State;typedef struct _Work Work;struct _Work{ State *pCurrentState; void (*SetState)(Work *pWork, void *pState); int mHour; void (*SetHour)(Work *pWork, int Hour); int (*GetHour)(Work *pWork); rt_bool_t mFinish; void (*SetFinish)(Work *pWork, rt_bool_t Finish); rt_bool_t (*GetFinish)(Work *pWork); void (*WriteProgram)(Work *pWork); void (*Delete)(void *pWork);};extern Work *WorkCreate(void);#endif#include "Work.h"static void SetState(Work *pWork, void *pState){ pWork->pCurrentState = pState;}static void SetHour(Work *pWork, int Hour){ pWork->mHour = Hour;}static int GetHour(Work *pWork){ return pWork->mHour;}static void SetFinish(Work *pWork, rt_bool_t Finish){ pWork->mFinish = Finish;}static rt_bool_t GetFinish(Work *pWork){ return pWork->mFinish;}static void WriteProgram(Work *pWork){ pWork->pCurrentState->WriteProgram(pWork->pCurrentState, pWork);}static void Delete(void *pWork){ rt_free(pWork);}Work *WorkCreate(void){ Work *pWork = (Work *)rt_malloc(sizeof(Work)); pWork->pCurrentState = (State *)ForenoonStateCreate(sizeof(ForenoonState)); pWork->SetState = SetState; pWork->GetHour = GetHour; pWork->SetHour = SetHour; pWork->mFinish = RT_FALSE; pWork->SetFinish = SetFinish; pWork->GetFinish = GetFinish; pWork->WriteProgram = WriteProgram; pWork->Delete = Delete; return pWork;}#ifndef __STATE_H__#define __STATE_H__#include "rtthread.h"#include "Work.h"//抽像状态类typedef struct _Work Work;typedef struct _State State;struct _State{ void (*WriteProgram)(void *pState, Work *pWork); void (*Delete)(void *pState);};//上午工作状态类typedef struct _ForenoonState ForenoonState;struct _ForenoonState{ State mState;};//中午工作状态类typedef struct _NoonState NoonState;struct _NoonState{ State mState;};//下午工作状态类typedef struct _AfternoonState AfternoonState;struct _AfternoonState{ State mState;};//晚间工作状态类typedef struct _EveningState EveningState;struct _EveningState{ State mState;};//睡眠工作状态typedef struct _SleepingState SleepingState;struct _SleepingState{ State mState;};//下班工作状态typedef struct _ResetState ResetState;struct _ResetState{ State mState;};extern ResetState *ResetStateCreate(rt_size_t Size);extern SleepingState *SleepingStateCreate(rt_size_t Size);extern EveningState *EveningStateCreate(rt_size_t Size);extern AfternoonState *AfternoonStateCreate(rt_size_t Size);extern NoonState *NoonStateCreate(rt_size_t Size);extern ForenoonState *ForenoonStateCreate(rt_size_t Size);extern State *StateCreate(rt_size_t Size);#endif#include "State.h"#include "Work.h"//抽像工作状态函数组static void StateDelete(void *pState){ rt_free(pState);}State *StateCreate(rt_size_t Size){ State *pState = (State *)rt_malloc(Size); pState->Delete = StateDelete; return pState;}//上午工作状态函数组static void ForenoonWriteProgram(void *pForenoon, Work *pWork){ if(pWork->mHour < 12) { rt_kprintf(" 当前时间:%d点 上午工作,精神百倍\n", pWork->mHour); } else { pWork->SetState(pWork, NoonStateCreate(sizeof(NoonState))); pWork->WriteProgram(pWork); }}ForenoonState *ForenoonStateCreate(rt_size_t Size){ ForenoonState *pForenoonState = (ForenoonState *)StateCreate(Size); pForenoonState->mState.WriteProgram = ForenoonWriteProgram; return pForenoonState;}//中午工作状态函数组static void NoonWriteProgram(void *pNoon, Work *pWork){ if(pWork->mHour < 13) { rt_kprintf(" 当前时间:%d点 饿了,吃完饭,犯困,午休\n", pWork->mHour); } else { pWork->SetState(pWork, AfternoonStateCreate(sizeof(AfternoonState))); pWork->WriteProgram(pWork); }}NoonState *NoonStateCreate(rt_size_t Size){ NoonState *pNoonState = (NoonState *)StateCreate(Size); pNoonState->mState.WriteProgram = NoonWriteProgram; return pNoonState;}//下午工作状态函数组static void AfternoonWriteProgram(void *pAfternoon, Work *pWork){ if(pWork->mHour < 17) { rt_kprintf(" 当前时间:%d点 下午状态还不错,继续努力\n", pWork->mHour); } else { pWork->SetState(pWork, EveningStateCreate(sizeof(NoonState))); pWork->WriteProgram(pWork); }}AfternoonState *AfternoonStateCreate(rt_size_t Size){ AfternoonState *pAfternoonState = (AfternoonState *)StateCreate(Size); pAfternoonState->mState.WriteProgram = AfternoonWriteProgram; return pAfternoonState;}//晚间工作状态函数组static void EveningWriteProgram(void *pEvening, Work *pWork){ if(pWork->mFinish) { pWork->SetState(pWork, ResetStateCreate(sizeof(ResetState))); pWork->WriteProgram(pWork); } else if(pWork->mHour < 21) { rt_kprintf(" 当前时间:%d点 加班,疲累之极\n", pWork->mHour); } else { pWork->SetState(pWork, SleepingStateCreate(sizeof(SleepingState))); pWork->WriteProgram(pWork); }}EveningState *EveningStateCreate(rt_size_t Size){ EveningState *pEveningState = (EveningState *)StateCreate(Size); pEveningState->mState.WriteProgram = EveningWriteProgram; return pEveningState;}//睡眠工作状态函数组static void SleepingWriteProgram(void *pSleeping, Work *pWork){ rt_kprintf(" 当前时间:%d点 不行了,睡着了\n", pWork->mHour);}SleepingState *SleepingStateCreate(rt_size_t Size){ SleepingState *pSleepingState = (SleepingState *)StateCreate(Size); pSleepingState->mState.WriteProgram = SleepingWriteProgram; return pSleepingState;}//下班工作状态函数组static void ResetWriteProgram(void *pReset, Work *pWork){ rt_kprintf(" 当前时间:%d点 下班回家\n", pWork->mHour);}ResetState *ResetStateCreate(rt_size_t Size){ ResetState *pResetState = (ResetState *)StateCreate(Size); pResetState->mState.WriteProgram = ResetWriteProgram; return pResetState;}#ifndef __STATEUSER_H__#define __STATEUSER_H__#include "rtthread.h"#include "finsh.h"#include "State.h"#include "Work.h"#endif #include "StateUser.h"//客户端void StateUser(void){ Work *pEmergencyProject = WorkCreate(); pEmergencyProject->SetHour(pEmergencyProject, 9); pEmergencyProject->WriteProgram(pEmergencyProject); pEmergencyProject->SetHour(pEmergencyProject, 10); pEmergencyProject->WriteProgram(pEmergencyProject); pEmergencyProject->SetHour(pEmergencyProject, 11); pEmergencyProject->WriteProgram(pEmergencyProject); pEmergencyProject->SetHour(pEmergencyProject, 12); pEmergencyProject->WriteProgram(pEmergencyProject); pEmergencyProject->SetHour(pEmergencyProject, 13); pEmergencyProject->WriteProgram(pEmergencyProject); pEmergencyProject->SetHour(pEmergencyProject, 14); pEmergencyProject->WriteProgram(pEmergencyProject); pEmergencyProject->SetHour(pEmergencyProject, 15); pEmergencyProject->WriteProgram(pEmergencyProject); pEmergencyProject->SetHour(pEmergencyProject, 16); pEmergencyProject->WriteProgram(pEmergencyProject); pEmergencyProject->SetHour(pEmergencyProject, 17); pEmergencyProject->mFinish = RT_TRUE;// pEmergencyProject->mFinish = RT_FALSE; pEmergencyProject->WriteProgram(pEmergencyProject); pEmergencyProject->SetHour(pEmergencyProject, 19); pEmergencyProject->WriteProgram(pEmergencyProject); pEmergencyProject->SetHour(pEmergencyProject, 22); pEmergencyProject->WriteProgram(pEmergencyProject); }FINSH_FUNCTION_EXPORT(StateUser, State Modle);