• Suppose an object is always in one of several known states
• The state an object is in determines the behavior of several methods
• Could use if/case statements in each method
• Better solution: state pattern
Have a reference to a state object
– Normally, state object doesn’t contain any fields
– Change state: change state object
– Methods delegate to state object
#include <iostream>
using namespace std;
class Machine {
class State* current;
public:
Machine();
void setCurrent( State* s ) { current = s; }
void on();
void off();
};
class State {
public:
virtual void on( Machine* m ) { cout << " already ON\n"; }
virtual void off( Machine* m ) { cout << " already OFF\n"; }
};
void Machine::on() { current->on( this ); }
void Machine::off() { current->off( this ); }
class ON : public State {
public:
ON() { cout << " ON-ctor "; };
~ON() { cout << " dtor-ON\n"; };
void off( Machine* m );
};
void ON::off(Machine* m)
{
cout << " going from ON to OFF";
m->setCurrent( new OFF() );
delete this;
}
class OFF : public State {
public:
OFF() { cout << " OFF-ctor "; };
~OFF() { cout << " dtor-OFF\n"; };
void on( Machine* m ) {
cout << " going from OFF to ON";
m->setCurrent( new ON() );
delete this;
}
};
Machine::Machine()
{
current = new OFF(); cout << '\n';
}
void main( void ) {
void (Machine::*ptrs[])() = { &Machine::off, &Machine::on };
Machine fsm;
int num;
while (1) {
cout << "Enter 0/1: ";
cin >> num;
(fsm.*ptrs[num])();
} }