-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathobserver pattern
More file actions
71 lines (63 loc) · 1.19 KB
/
observer pattern
File metadata and controls
71 lines (63 loc) · 1.19 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
61
62
63
64
65
66
67
68
69
70
71
// ConsoleApplication16.cpp : 이 파일에는 'main' 함수가 포함됩니다. 거기서 프로그램 실행이 시작되고 종료됩니다.
//
#include "pch.h"
#include <iostream>
#include <vector>
using namespace std;
class Iobserver {
public:
virtual void Update(int n) = 0;
};
class Column : public Iobserver{
public:
void Update(int n) {
cout << "Column : ";
for (int i = 0; i < n; ++i)
{
cout << "!";
}
cout << endl;
}
};
class Row : public Iobserver {
public:
void Update(int n) {
cout << "Row : ";
for (int i = 0; i < n; ++i)
{
cout << ")";
}
cout << endl;
}
};
class Table {
public:
vector<Iobserver*> obs;
void Add(Iobserver* p) { obs.push_back(p); }
void Erase(Iobserver* p) { }
void Edit() {
while (true) {
int data;
cout << "Data를 넣으세요" << endl;
cin >> data;
for (int i = 0; i < obs.size(); ++i) {
obs[i]->Update(data);
}
}
}
};
int main()
{
Table* pTable = new Table();
Column* pColumn = new Column();
Row* pRow = new Row();
pTable->Add(pColumn);
pTable->Add(pRow);
pTable->Edit();
if (pTable) delete pTable;
if (pColumn) delete pColumn;
if (pRow) delete pRow;
pTable = nullptr;
pColumn = nullptr;
pRow = nullptr;
}