-
Notifications
You must be signed in to change notification settings - Fork 80
/
G25_Classic_Observer.cpp
200 lines (148 loc) · 5.18 KB
/
G25_Classic_Observer.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**************************************************************************************************
*
* \file G25_Classic_Observer.cpp
* \brief Guideline 25: Apply Observers as an Abstract Notification Mechanism
*
* Copyright (C) 2022 Klaus Iglberger - All Rights Reserved
*
* This file is part of the supplemental material for the O'Reilly book "C++ Software Design"
* (https://www.oreilly.com/library/view/c-software-design/9781098113155/).
*
**************************************************************************************************/
//---- <Observer.h> -------------------------------------------------------------------------------
template< typename Subject, typename StateTag >
class Observer
{
public:
virtual ~Observer() = default;
virtual void update( Subject const& subject, StateTag property ) = 0;
};
//---- <Person.h> ---------------------------------------------------------------------------------
//#include <Observer.h>
#include <string>
#include <set>
class Person
{
public:
enum StateChange
{
forenameChanged,
surnameChanged,
addressChanged
};
using PersonObserver = Observer<Person,StateChange>;
explicit Person( std::string forename, std::string surname )
: forename_{ std::move(forename) }
, surname_{ std::move(surname) }
{}
bool attach( PersonObserver* observer );
bool detach( PersonObserver* observer );
void notify( StateChange property );
void forename( std::string newForename );
void surname ( std::string newSurname );
void address ( std::string newAddress );
std::string const& forename() const { return forename_; }
std::string const& surname () const { return surname_; }
std::string const& address () const { return address_; }
private:
std::string forename_;
std::string surname_;
std::string address_;
std::set<PersonObserver*> observers_;
};
//---- <Person.cpp> -------------------------------------------------------------------------------
//#include <Person.h>
void Person::forename( std::string newForename )
{
forename_ = std::move(newForename);
notify( forenameChanged );
}
void Person::surname( std::string newSurname )
{
surname_ = std::move(newSurname);
notify( surnameChanged );
}
void Person::address( std::string newAddress )
{
address_ = std::move(newAddress);
notify( addressChanged );
}
bool Person::attach( PersonObserver* observer )
{
auto [pos,success] = observers_.insert( observer );
return success;
}
bool Person::detach( PersonObserver* observer )
{
return ( observers_.erase( observer ) > 0U );
}
void Person::notify( StateChange property )
{
// This formulation makes sure detach() operations
// can be detected during the iteration
for( auto iter=begin(observers_); iter!=end(observers_); )
{
auto const pos = iter++;
(*pos)->update(*this,property);
}
}
//---- <NameObserver.h> ---------------------------------------------------------------------------
//#include <Observer.h>
//#include <Person.h>
class NameObserver : public Observer<Person,Person::StateChange>
{
public:
void update( Person const& person, Person::StateChange property ) override;
};
//---- <NameObserver.cpp> ----------------
//#include <NameObserver.h>
void NameObserver::update( Person const& person, Person::StateChange property )
{
if( property == Person::forenameChanged ||
property == Person::surnameChanged )
{
// ... Respond to changed name
}
}
//---- <AddressObserver.h> ------------------------------------------------------------------------
//#include <Observer.h>
//#include <Person.h>
class AddressObserver : public Observer<Person,Person::StateChange>
{
public:
void update( Person const& person, Person::StateChange property ) override;
};
//---- <AddressObserver.cpp> ----------------------------------------------------------------------
//#include <AddressObserver.h>
void AddressObserver::update( Person const& person, Person::StateChange property )
{
if( property == Person::addressChanged ) {
// ... Respond to changed address
}
}
//---- <Main.cpp> ---------------------------------------------------------------------------------
//#include <AddressObserver.h>
//#include <NameObserver.h>
//#include <Person.h>
#include <cstdlib>
int main()
{
NameObserver nameObserver;
AddressObserver addressObserver;
Person homer( "Homer" , "Simpson" );
Person marge( "Marge" , "Simpson" );
Person monty( "Montgomery", "Burns" );
// Attaching observers
homer.attach( &nameObserver );
marge.attach( &addressObserver );
monty.attach( &addressObserver );
// Updating information on Homer Simpson
homer.forename( "Homer Jay" ); // Adding his middle name
// Updating information on Marge Simpson
marge.address( "712 Red Bark Lane, Henderson, Clark County, Nevada 89011" );
// Updating information on Montgomery Burns
monty.address( "Springfield Nuclear Power Plant" );
// Detaching observers
homer.detach( &nameObserver );
return EXIT_SUCCESS;
}