-
Notifications
You must be signed in to change notification settings - Fork 0
/
20_state.cpp
164 lines (147 loc) · 4.32 KB
/
20_state.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <string>
#include <fstream>
//#########################################
class CHandler {
private:
/* data */
public:
CHandler() { std::cout << __FUNCTION__ << "\n"; };
virtual ~CHandler() { std::cout << __FUNCTION__ << "\n"; };
virtual void output(std::string str){};
virtual void finalize(){};
};
class CTerminalHandler : public CHandler {
private:
std::ofstream &_ofs;
public:
CTerminalHandler(std::ofstream &ofs) : _ofs(ofs) {
std::cout << __FUNCTION__ << "\n";
};
virtual ~CTerminalHandler() { std::cout << __FUNCTION__ << "\n"; };
void output(std::string str) {
std::cout << "CTerminalHandler::" << __FUNCTION__ << "\n";
_ofs << str << std::endl;
};
void finalize() {
std::cout << "CTerminalHandler::" << __FUNCTION__ << "\n";
_ofs.close();
};
};
//#########################################
class CContext {
private:
int _condition;
CHandler *_hndl;
public:
CContext(CHandler *hndl) : _condition(0) {
_hndl = hndl;
std::cout << __FUNCTION__ << "\n";
};
virtual ~CContext() { std::cout << __FUNCTION__ << "\n"; };
void Context_Method_1() {
_condition = 1;
std::cout << __FUNCTION__ << " : [condition] ->" << _condition << "\n";
_hndl->output(std::string{__FUNCTION__});
}
void Context_Method_2() {
_condition = 2;
std::cout << __FUNCTION__ << " : [condition] ->" << _condition << "\n";
_hndl->output(std::string{__FUNCTION__});
}
};
//#########################################
// State pattern!!!
//#########################################
class State {
private:
protected:
public:
State(){};
virtual ~State(){};
};
//#########################################
// State pattern implecated for each app
//#########################################
// @ interface
class IStateApp : public State {
private:
protected:
static IStateApp *toStateA();
static IStateApp *toStateB();
public:
IStateApp() : State(){};
virtual ~IStateApp(){};
static IStateApp *Initialize();
virtual IStateApp *EventA(CContext *ctx) = 0;
virtual IStateApp *EventB(CContext *ctx) = 0;
};
//#########################################
// ConcreteStates
//#########################################
class State_A : public IStateApp {
private:
public:
State_A() : IStateApp() { std::cout << "State_A::" << __FUNCTION__ << "\n"; };
virtual ~State_A() { std::cout << "State_A::" << __FUNCTION__ << "\n"; };
//-----------------------------
IStateApp *EventA(CContext *ctx) {
std::cout << "State_A::" << __func__ << std::endl;
ctx->Context_Method_1();
return toStateB();
};
//-----------------------------
IStateApp *EventB(CContext *ctx) {
std::cout << "State_A::" << __func__ << std::endl;
return this;
};
//-----------------------------
};
class State_B : public IStateApp {
private:
public:
State_B() : IStateApp() { std::cout << "State_B::" << __FUNCTION__ << "\n"; };
virtual ~State_B() { std::cout << "State_B::" << __FUNCTION__ << "\n"; };
//-----------------------------
IStateApp *EventA(CContext *ctx) {
std::cout << "State_B::" << __func__ << std::endl;
return this;
};
//-----------------------------
IStateApp *EventB(CContext *ctx) {
std::cout << "State_B::" << __func__ << std::endl;
ctx->Context_Method_2();
return toStateA();
};
//-----------------------------
};
IStateApp *IStateApp::Initialize() { return toStateA(); };
IStateApp *IStateApp::toStateA() {
static State_A state;
return &state;
};
IStateApp *IStateApp::toStateB() {
static State_B state;
return &state;
};
//#########################################
// main
//#########################################
int main(int argc, char const *argv[]) {
std::ofstream ofs{"output.log"};
CHandler *hndl = new CTerminalHandler{ofs};
CContext ctx{hndl};
// IStateApp *p = new State_A(&ctx);
std::cout << "-------" << std::endl;
IStateApp *p = IStateApp::Initialize();
std::cout << "-------" << std::endl;
p = p->EventA(&ctx);
std::cout << "-------" << std::endl;
p = p->EventA(&ctx);
std::cout << "-------" << std::endl;
p = p->EventB(&ctx);
std::cout << "-------" << std::endl;
p = p->EventB(&ctx);
std::cout << "-------" << std::endl;
return 0;
}