/
main.cpp
166 lines (150 loc) · 4.11 KB
/
main.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
165
166
/*-----------------------------------------------------------------------*
* Simulation Model "Greeting"
*
* filename - main.cpp
* Author: Vasily I. Gurianov
* Create: 2017-07-22, Last modification: 2022-06-25
* Version: 2.0
* Testing: Dev C++
*
* Base classes definitions for Application by SimpleExample
*
*-----------------------------------------------------------------------*/
#include <iostream>
using namespace std;
/*-----------------------------------------------------------------------*
* name - class Component
*
* Abstract class for Composite pattern
* Concept = Subject of communication
*-----------------------------------------------------------------------*/
class Component {
private:
public:
string name;
string probe; //* <<Tool>> {Concept = Probe}
Component *next;
void virtual Run() = 0;
// if this operation put to Component, then Component is Subject of communication
string virtual getMessage() { //# {Concept = Send message} #//
return 0;
}
void virtual putMessage(string s) { //$ {Concept = Resept message} $//
}
};
/*-----------------------------------------------------------------------*
* name - class Leaf
*
* Leaf class for Composite pattern
* Concept = Human
*-----------------------------------------------------------------------*/
class Leaf : public Component {
class DialogList { // Concept = Dialogue protocol, i.e. general knowledge
public:
string value;
DialogList *next;
DialogList(){
value = ""; next = NULL;
}
};
private:
DialogList *memory, *cm;
string msg;
public:
Leaf(string s) {
name = s;
memory = new DialogList;
cm = memory;
cm->value = "Good morning. How are you?.";
cm->next = new DialogList;
cm->next->value = "Not bad, thank you";
cm = memory;
this->next = NULL;
}
void virtual Run() {// <<Exist>>
if (msg.size() != 0) cm = cm->next;
}
string virtual getMessage() { //# {Concept = Send message} #//
string rr;
if (cm != NULL) rr = cm->value; else rr = "<end dialog>";
return rr;
}
void virtual putMessage(string s) { //$ {Concept = Resept message} $//
this->msg = s;
}
};
/*-----------------------------------------------------------------------*
* name - class Composite
*
* Composite class for Composite pattern
* Concept = Dialog space
*
*-----------------------------------------------------------------------*/
class Composite : public Component {
private:
protected:
public:
Component *place; // Concept = The speaker place
Composite() {
}
};
/*-----------------------------------------------------------------------*
* name - class Node
*
* Concrete class for Composite pattern
* Concept = Greeting
*-----------------------------------------------------------------------*/
class Node : public Composite {
private:
string msg;
void nextItem(){
place = place->next;
}
public:
Node() {
place = new Leaf("Goldsmith");
place->next = new Leaf("Brown");
msg="";
}
//
void Run() { // <<Exist>>
if (place != NULL) {
place->putMessage(msg); place->Run(); msg = place->getMessage();
probe = place->name + ": " + msg; // measurement (observation)
this->nextItem();
} else probe = "close dialog";
}
};
/*-----------------------------------------------------------------------*
* name - class Root
*
* Concrete class for Composite pattern
* Concept = The street (Define initial and boundary conditions for Node class)
*
*-----------------------------------------------------------------------*/
class Root : public Composite {
private:
public:
Root() {
this->place = new Node();
}
void Run() { // <<Exist>>
this->place->Run();
this->probe = this->place->probe;
}
};
// -----------------------------------------------------------------------
// main enter point
int main(int argc, char** argv) {
Composite *root = new Root();
cout << "Step 1 " << endl;
root->Run();
cout << root->probe << endl;
cout << "Step 2 " << endl;
root->Run();
cout << root->probe << endl;
cout << "Step 3 " << endl;
root->Run();
cout << root->probe << endl;
return 0;
}