Created
December 12, 2014 03:19
-
-
Save tylerwal/bda768180fda032c862f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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"; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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