C 언어 구현 상태 디자인 모드 (http://www.itnose.net)
6673 단어 .net
#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
#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;
void (*EndState_constructor)(struct EndState* /* pThis */);
};
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
");
}
void AnaphaseState_Action(struct State* pThis,struct War* pWar)
{
if(pWar->War_getDays(pWar) < 30)
{
printf("333 : %u
",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
",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
",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
",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
",pState);
return pState;
}
struct State* getMetaphaseState(void)
{
NEW(State,pState,MetaphaseState);
printf("new %p
",pState);
return pState;
}
struct State* getAnaphaseState(void)
{
NEW(State,pState,AnaphaseState);
printf("new %p
",pState);
return pState;
}
struct State* getEndState(void)
{
NEW(State,pState,EndState);
printf("new %p
",pState);
return pState;
}
/*
test
*/
int main(void)
{
NEW(War,pWar,War);
printf("init
");
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;
}
문장 출처: http://www.itnose.net/detail/6038660.html 더 많은 글: http://www.itnose.net/type/61.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
AS를 통한 Module 개발1. ModuleLoader 사용 2. IModuleInfo 사용 ASModuleOne 모듈...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.