-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.h
41 lines (31 loc) · 763 Bytes
/
state.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once
#include <iostream>
// the abstract state class
class State {
public:
virtual void handle() = 0;
virtual ~State() {}
};
// the concrete state class A
class ConcreteStateA : public State {
public:
void handle() override { std::cout << "Handling state A" << std::endl; }
};
// the concrete state class B
class ConcreteStateB : public State {
public:
void handle() override { std::cout << "Handling state B" << std::endl; }
};
// the context class, providing the state switch interface
class Context {
private:
State* state;
public:
Context(State* state) : state(state) {}
void setState(State* newState) {
if (newState != nullptr) {
this->state = newState;
}
}
void request() { this->state->handle(); }
};