-
Notifications
You must be signed in to change notification settings - Fork 80
/
G25_Modern_Observer.cpp
175 lines (134 loc) · 4.4 KB
/
G25_Modern_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
/**************************************************************************************************
*
* \file G25_Modern_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> -------------------------------------------------------------------------------
#include <functional>
template< typename Subject, typename StateTag >
class Observer
{
public:
using OnUpdate = std::function<void(Subject const&,StateTag)>;
// No virtual destructor necessary
explicit Observer( OnUpdate onUpdate )
: onUpdate_{ std::move(onUpdate) }
{
// Possibly respond on an invalid/empty std::function instance
}
// Non-virtual update function
void update( Subject const& subject, StateTag property )
{
onUpdate_( subject, property );
}
private:
OnUpdate onUpdate_;
};
//---- <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);
}
}
//---- <Main.cpp> ---------------------------------------------------------------------------------
//#include <Observer.h>
//#include <Person.h>
#include <cstdlib>
void propertyChanged( Person const& person, Person::StateChange property )
{
if( property == Person::forenameChanged ||
property == Person::surnameChanged )
{
// ... Respond to changed name
}
}
int main()
{
using PersonObserver = Observer<Person,Person::StateChange>;
PersonObserver nameObserver( propertyChanged );
PersonObserver addressObserver(
[/*captured state*/]( Person const& person, Person::StateChange property ){
if( property == Person::addressChanged )
{
// ... Respond to changed address
}
} );
Person homer( "Homer" , "Simpson" );
Person marge( "Marge" , "Simpson" );
Person monty( "Montgomery", "Burns" );
// Attaching observers
homer.attach( &nameObserver );
marge.attach( &addressObserver );
monty.attach( &addressObserver );
// ...
return EXIT_SUCCESS;
}