Skip to content

Instantly share code, notes, and snippets.

@tylerwal
Created December 12, 2014 03:19
Show Gist options
  • Select an option

  • Save tylerwal/bda768180fda032c862f to your computer and use it in GitHub Desktop.

Select an option

Save tylerwal/bda768180fda032c862f to your computer and use it in GitHub Desktop.
#include "StatePattern.h"
#include <iostream>
int main()
{
LogicFunction();
}
void LogicFunction()
{
Container container;
IState * stateA = container.getStateA();
stateA->Method();
}
/* Container */
Container::Container()
{
StateA state(this);
stateA = &state;
setCurrentState(stateA);
};
void Container::setCurrentState(IState * state)
{
currentState = state;
};
IState * Container::getCurrentState()
{
return currentState;
};
IState * Container::getStateA()
{
return stateA;
};
IState * Container::getStateB()
{
return stateB;
};
/* State A */
StateA::StateA(IContainer * container)
{
};
void StateA::Method()
{
std::cout << "State A Method";
};
/* State B */
StateB::StateB(IContainer * container)
{
};
void StateB::Method()
{
std::cout << "State B Method";
};
void LogicFunction();
class IState;
class IContainer
{
public:
virtual void setCurrentState(IState * state) = 0;
virtual IState * getCurrentState() = 0;
virtual IState * getStateA() = 0;
virtual IState * getStateB() = 0;
};
class IState
{
public:
IState(){};
virtual void Method() = 0;
};
class StateA;
class StateB;
class Container : IContainer
{
private:
IState * currentState;
IState * stateA;
IState * stateB;
public:
Container();
void setCurrentState(IState * state);
IState * getCurrentState();
IState * getStateA();
IState * getStateB();
};
class StateA : public IState
{
public:
StateA(IContainer * thermostat);
void Method();
};
class StateB : public IState
{
public:
StateB(IContainer * thermostat);
void Method();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment