-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver2.cpp
More file actions
110 lines (90 loc) · 1.7 KB
/
observer2.cpp
File metadata and controls
110 lines (90 loc) · 1.7 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
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
/**
* observer.h
* Implemented by Blueprint Technologies, Inc.
*
* \todo
*/
#include <iostream>
#include <vector>
using namespace std;
/**
* Defines an updating interface for objects that should be
* notified of changes in a subject.
*/
class Ibserver
{
public:
virtual void update() = 0;
};
/**
* Maintains a reference to a ConcreteSubject object. Stores
* state that should stay consistent with the subject's.
* Implements the Ibserver updating interface to keep its
* state consistent with the subject's.
*/
class ConcreteObserver: public Ibserver
{
public:
virtual void update()
{
cout << "ConcreteObserver::update()" << endl;
};
};
/**
* Knows its observers. Any number of Ibserver objects may
* observe a subject. Provides an interface for attaching and
* detaching Ibserver objects.
*/
class Subject
{
private:
vector< Ibserver* > observers;
public:
void attach( Ibserver* observer )
{
vector< Ibserver* >::iterator i;
int contained = 0;
for( i = observers.begin(); i != observers.end(); ++i )
{
if( *i == observer )
{
contained = 1;
}
}
if( !contained )
{
observers.push_back( observer );
}
};
void detach( Ibserver* observer )
{
vector< Ibserver* >::iterator i;
for( i = observers.begin(); i != observers.end(); ++i )
{
if( *i == observer )
{
observers.erase(i);
return;
}
}
};
void notify()
{
vector< Ibserver* >::iterator i;
for( i = observers.begin(); i != observers.end(); ++i )
{
(*i) -> update();
}
};
};
/**
* Stores state of interest to ConcreteObserver objects. Sends a
* notification to its observers when its state changes.
*/
class ConcreteSubject: public Subject
{
};
int main()
{
return 0;
}