-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample.h
More file actions
60 lines (43 loc) · 1.39 KB
/
Copy pathConceptualExample.h
File metadata and controls
60 lines (43 loc) · 1.39 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// ===========================================================================
// ConceptualExample.h // State Pattern
// ===========================================================================
#pragma once
#include <memory>
#include <string>
#include <string_view>
namespace ConceptualExample {
class Context;
class StateBase
{
public:
virtual ~StateBase() = default;
virtual void handle(Context& context) = 0;
virtual std::string_view getDescription() const noexcept = 0;
};
class Context
{
private:
std::unique_ptr<StateBase> m_state;
public:
explicit Context(std::unique_ptr<StateBase> state);
void setState(std::unique_ptr<StateBase> state);
void request();
};
class ConcreteStateA final : public StateBase
{
public:
void handle(Context& context) override;
[[nodiscard]]
std::string_view getDescription() const noexcept override { return "State A"; }
};
class ConcreteStateB final : public StateBase
{
public:
void handle(Context& context) override;
[[nodiscard]]
std::string_view getDescription() const noexcept override { return "State B"; }
};
}
// ===========================================================================
// End-of-File
// ===========================================================================