add example of state pattern in C++
This commit is contained in:
13
cpp/patterns/state/state.cpp
Normal file
13
cpp/patterns/state/state.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
#include "state.hpp"
|
||||
|
||||
// Delegate calls from State->changeState to Car->changeState
|
||||
void State::changeState(Car *c, State *s) { c->changeState(s);}
|
||||
|
||||
// Stopped state transitions
|
||||
void Stopped::Start(Car *c) { changeState(c, Running::instance());}
|
||||
void Stopped::Smash(Car *c) { changeState(c, Broken::instance());}
|
||||
|
||||
// Running state transitions
|
||||
void Running::Stop(Car *c) { changeState(c, Stopped::instance());}
|
||||
void Running::Smash(Car *c) { changeState(c, Burning::instance());}
|
||||
Reference in New Issue
Block a user