1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #include <iostream>
- using namespace std;
- class State;
- class ConcreteStateB;
- class ConcreteStateA;
- class State {
- public:
- virtual void OperationChangeState(Context*) = 0;
- virtual void OperationInterface(Context*) = 0;
- virtual ~State() { }
- protected:
- bool ChangeState(Context* con, State *st) {
- con->ChangeState(st);
- }
- };
- class ConcreteStateA :public State {
- public:
- void OperationChangeState(Context* con) {
- OperationInterface(con);
- this->ChangeState(con, new ConcreteStateB());
- }
- void OperationInterface(Context* con) {
- cout << "ConcreteStateA::OperationInterface..." << endl;
- }
- };
- class ConcreteStateB :public State {
- public:
- void OperationChangeState(Context* con) {
- OperationInterface(con);
- this->ChangeState(con, new ConcreteStateA());
- }
- void OperationInterface(Context* con) {
- cout << "ConcreteStateB::OperationInterface..." << endl;
- }
- };
- class Context {
- public:
- Context(State* st) {
- _st = st;
- }
- void OperationInterface() {
- _st->OperationInterface(this);
- }
- void OperationChangeState() {
- _st->OperationInterface(this);
- }
- private:
- friend class State;
- bool ChangeState(State* st) {
- _st = st;
- return true;
- }
- State *_st;
- };
- int main() {
- State *st = new ConcreteStateA();
- Context *con = new Context(st);
- con->OperationInterface();
- con->OperationInterface();
- con->OperationInterface();
- delete con;
- delete st;
- return 0;
- }
|