State 모드 (상태 모드) 학습
12947 단어 디자인 모드
#include <iostream>
#include <vector>
#include <assert.h>
using namespace std;
typedef enum
{
LOCKED,
UNLOCKED,
}State;
typedef enum
{
COIN,
PASS
}Event;
#if 0 // , ,switch/case , Action
class Turnstile
{
public:
int state = LOCKED;
void HandleEvent(Event event)
{
switch (state)
{
case LOCKED:
switch (event)
{
case COIN:
cout << "lock (event:coin)-> unlock" << endl;
state = UNLOCKED;
break;
case PASS:
cout << "Pass when locked, alarm!" << endl;
//state not change
break;
default:
break;
}
break;
case UNLOCKED:
switch (event)
{
case COIN:
cout << "unlock (event:coin)-> unlock" << endl;
//state not change
break;
case PASS:
cout << "unlock (event:pass)-> lock" << endl;
state = LOCKED;
break;
default:
break;
}
break;
default:
break;
}
}
};
int main(int argc, char** argv)
{
Turnstile turnstile;
turnstile.state = LOCKED;
turnstile.HandleEvent(PASS);
assert(turnstile.state == LOCKED);
turnstile.state = UNLOCKED;
turnstile.HandleEvent(PASS);
assert(turnstile.state == LOCKED);
}
#endif
#if 0// , ,Action 。 Action , , 。 Action OCP, OCP
class AbstractTurnstile
{
public:
int state = LOCKED;
virtual void Unlock() = 0;
virtual void Lock() = 0;
virtual void Alarm() = 0;
virtual void ThankYou() = 0;
void HandleEvent(Event event)
{
switch (state)
{
case LOCKED:
switch (event)
{
case COIN:
Unlock();
state = UNLOCKED;
break;
case PASS:
Alarm();
//state not change
break;
default:
break;
}
break;
case UNLOCKED:
switch (event)
{
case COIN:
ThankYou();
//state not change
break;
case PASS:
Lock();
state = LOCKED;
break;
default:
break;
}
break;
default:
break;
}
}
};
class ACustomerTurnstile : public AbstractTurnstile
{
virtual void Unlock() { cout << "lock (event:coin)-> unlock" << endl; }
virtual void Lock() { cout << "unlock (event:pass)-> lock" << endl; }
virtual void Alarm() { cout << "Pass when locked, alarm!" << endl; }
virtual void ThankYou() { cout << "unlock (event:coin)-> unlock" << endl; }
};
int main(int argc, char** argv)
{
AbstractTurnstile *pTurnstile = new ACustomerTurnstile;
pTurnstile->state = LOCKED;
pTurnstile->HandleEvent(PASS);
assert(pTurnstile->state == LOCKED);
pTurnstile->state = UNLOCKED;
pTurnstile->HandleEvent(PASS);
assert(pTurnstile->state == LOCKED);
delete pTurnstile;
}
#endif
#if 0// : , Action TurnstileAction , Turnstile , 。
// (main ) TurnstileAction
class TurnstileAction
{
public:
virtual void Unlock() = 0;
virtual void Lock() = 0;
virtual void Alarm() = 0;
virtual void ThankYou() = 0;
};
class Turnstile
{
public:
int state = LOCKED;
TurnstileAction* pAction = NULL;
Turnstile(TurnstileAction* pAct)
{
pAction = pAct;
}
void HandleEvent(Event event)
{
switch (state)
{
case LOCKED:
switch (event)
{
case COIN:
pAction->Unlock();
state = UNLOCKED;
break;
case PASS:
pAction->Alarm();
//state not change
break;
default:
break;
}
break;
case UNLOCKED:
switch (event)
{
case COIN:
pAction->ThankYou();
//state not change
break;
case PASS:
pAction->Lock();
state = LOCKED;
break;
default:
break;
}
break;
default:
break;
}
}
};
int main(int argc, char** argv)
{
// Action
class ACustomerAction : public TurnstileAction
{
virtual void Unlock() { cout << "lock (event:coin)-> unlock" << endl; }
virtual void Lock() { cout << "unlock (event:pass)-> lock" << endl; }
virtual void Alarm() { cout << "Pass when locked, alarm!" << endl; }
virtual void ThankYou() { cout << "unlock (event:coin)-> unlock" << endl; }
};
// Action Turnstile
ACustomerAction action;
Turnstile turnstile(&action);
// Turnstile
turnstile.state = LOCKED;
turnstile.HandleEvent(PASS);
assert(turnstile.state == LOCKED);
turnstile.state = UNLOCKED;
turnstile.HandleEvent(PASS);
assert(turnstile.state == LOCKED);
}
#endif
#if 0// , , TurnstileAction , , OCP
/*// : Turnstile
typedef void(*Action)();
class TurnstileAction
{
public:
static void Unlock() { cout << "lock (event:coin)-> unlock" << endl; }
static void Lock() { cout << "unlock (event:pass)-> lock" << endl; }
static void Alarm() { cout << "Pass when locked, alarm!" << endl; }
static void ThankYou() { cout << "unlock (event:coin)-> unlock" << endl; }
};
class Transition
{
public:
Transition(int inCurrentState, int inEvent, int inNewState, Action inAction) :
currentState(inCurrentState), event(inEvent), newState(inNewState), action(inAction)
{;}
int currentState;
int event;
int newState;
Action action;
};
class Turnstile
{
public:
int state = LOCKED;
vector<Transition> trans;
Turnstile()
{
// : , , ,
AddTransition(LOCKED, COIN, UNLOCKED, TurnstileAction::Unlock);
AddTransition(LOCKED, PASS, LOCKED, TurnstileAction::Alarm);
AddTransition(UNLOCKED, COIN, UNLOCKED, TurnstileAction::ThankYou);
AddTransition(UNLOCKED, PASS, LOCKED, TurnstileAction::Lock);
}
void HandleEvent(Event event)
{
for (unsigned int i = 0; i < trans.size(); i++)
{
Transition* pTrans = &(trans[i]);
if (state == pTrans->currentState && event == pTrans->event)
{
state = pTrans->newState;
pTrans->action();
}
}
}
private:
void AddTransition(int currentState, int event, int newState, Action action)
{
trans.push_back(Transition(currentState, event, newState, action));
}
};
*/
// : , ( ,Action )
// ,
typedef void(*Action)();
class Transition
{
public:
Transition(int inCurrentState, int inEvent, int inNewState, Action inAction) :
currentState(inCurrentState), event(inEvent), newState(inNewState), action(inAction)
{ ; }
int currentState;
int event;
int newState;
Action action;
};
class StateMachine
{
public:
int state;
vector<Transition> trans;
void HandleEvent(Event event)
{
for (unsigned int i = 0; i < trans.size(); i++)
{
Transition* pTrans = &(trans[i]);
if (state == pTrans->currentState && event == pTrans->event)
{
state = pTrans->newState;
pTrans->action();
}
}
}
protected:
void AddTransition(int currentState, int event, int newState, Action action)
{
trans.push_back(Transition(currentState, event, newState, action));
}
};
// ,
class Turnstile : public StateMachine
{
public:
// Action
static void Unlock() { cout << "lock (event:coin)-> unlock" << endl; }
static void Lock() { cout << "unlock (event:pass)-> lock" << endl; }
static void Alarm() { cout << "Pass when locked, alarm!" << endl; }
static void ThankYou() { cout << "unlock (event:coin)-> unlock" << endl; }
//
Turnstile()
{
// : , , ,
AddTransition(LOCKED, COIN, UNLOCKED, Unlock);
AddTransition(LOCKED, PASS, LOCKED, Alarm);
AddTransition(UNLOCKED, COIN, UNLOCKED, ThankYou);
AddTransition(UNLOCKED, PASS, LOCKED, Lock);
}
};
int main(int argc, char** argv)
{
// Action Turnstile
Turnstile turnstile;
// Turnstile
turnstile.state = LOCKED;
turnstile.HandleEvent(PASS);
assert(turnstile.state == LOCKED);
turnstile.state = UNLOCKED;
turnstile.HandleEvent(PASS);
assert(turnstile.state == LOCKED);
}
#endif
#if 1/* , , , 。
:1. ,2. , , (State , State ,Turnstile ), / /
2 ,STATE , 。
,STATE C++ !Turnstile TurnstileLockedState , 。 ,
http://zhidao.baidu.com/link?url=5jzADbx87CLFt_NihyAO79FwIFGFwDWW5VZsOgH-AU77ZdC-Hga5PgVlVkJiu7aVigim3LSuZ9EdSiTCB2Ul1_
http://www.cnblogs.com/wanggary/archive/2011/04/21/2024117.html */
class Turnstile;
class TurnstileState;
class TurnstileLockedState;
class TurnstileUnlockedState;
// , ,
class TurnstileState
{
public:
virtual void CoinEvent(Turnstile* pTs) = 0;
virtual void PassEvent(Turnstile* pTs) = 0;
};
// , 。 , , Turnstile
class TurnstileLockedState : public TurnstileState
{
public:
virtual void CoinEvent(Turnstile* pTs);
virtual void PassEvent(Turnstile* pTs);
};
class TurnstileUnlockedState : public TurnstileState
{
public:
virtual void CoinEvent(Turnstile* pTs);
virtual void PassEvent(Turnstile* pTs);
};
//Context , Action
class Turnstile
{
private:
TurnstileState* pState = NULL;
TurnstileLockedState lockedState;
TurnstileUnlockedState unlockedState;
public:
Turnstile()
{
pState = &lockedState;
}
void SetState(State state)
{// ,
if (state == UNLOCKED)
{
pState = &unlockedState;
}
else
{
pState = &lockedState;
}
}
void CoinEvent()
{
pState->CoinEvent(this);
}
void PassEvent()
{
pState->PassEvent(this);
}
//Action , TurnstileAction
void Unlock() { cout << "lock (event:coin)-> unlock" << endl; }
void Lock() { cout << "unlock (event:pass)-> lock" << endl; }
void Alarm() { cout << "Pass when locked, alarm!" << endl; }
void ThankYou() { cout << "unlock (event:coin)-> unlock" << endl; }
};
// , Turnstile
void TurnstileLockedState::CoinEvent(Turnstile* pTs)
{
pTs->SetState(UNLOCKED);//
pTs->Unlock();// Action
}
void TurnstileLockedState::PassEvent(Turnstile* pTs)
{
// :
pTs->Alarm();// Action
}
void TurnstileUnlockedState::CoinEvent(Turnstile* pTs)
{
// :
pTs->ThankYou();// Action
}
void TurnstileUnlockedState::PassEvent(Turnstile* pTs)
{
pTs->SetState(LOCKED);//
pTs->Lock();// Action
}
int main(int argc, char** argv)
{
// Action Turnstile
Turnstile turnstile;
// Turnstile
turnstile.SetState(LOCKED);
turnstile.PassEvent();
//assert(turnstile.state == LOCKED);
turnstile.CoinEvent();//to be unlocked
turnstile.PassEvent();//to be locked
//assert(turnstile.state == LOCKED);
}
#endif
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
디자인 모델 의 공장 모델, 단일 모델자바 는 23 가지 디자인 모델 (프로 그래 밍 사상/프로 그래 밍 방식) 이 있 습 니 다. 공장 모드 하나의 공장 류 를 만들어 같은 인 터 페 이 스 를 실현 한 일부 종 류 를 인 스 턴 스 로 만 드 는 것...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.