Catégories

Design Patterns - Observer - Code source simple en C++

Temps de lecture : 4 minutes

Je lis toujours Head First Design Patterns. J'en suis au Capitre 2 à la page 37 où l'on parle du Observer. Ce coup-ci j'ai ajouté un peu plus de commentaires dans le code source de mon Observer en C++... Ça ne fait jamais de mal. Cela dit, avec le bouquin ça devrait aller.

Ce que j'ai retenu

Plus tard, je vais mettre ici mes notes à propos du Design Patterns en question. Là il n'y a rien car j'ai collé le code et zou, je suis passé à autre chose.

Le code

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
/*
See Chapter II, page 37
 
                Subject - Interface  ----------------->   Observer - Interface
                -------------------                                   ---------------------
                RegisterObserver                                        Update
                RemoveObserver
                NotifyObservers                                   ^
                                                                      |
                            ^                                         |                 
                            |                                         |
                            |                                         |
 
                Concrete Subject       <-----------------      Concrete Observer
                ----------------                                    ------------------
                RegisterObserver                                      Update
                RemoveObserver                                  ...
                NotifyObservers
                ...
 
*/
 
#ifdef _MSC_VER
  #define _CRTDBG_MAP_ALLOC
  #include <crtdbg.h>
#endif
 
#include <iostream>
#include <memory>
#include <list>
#include <vector>
#include <numeric> // accumulate
 
using namespace std;
 
// ----------------------------------------------------------------------------
class ObserverInterface{
public:
    virtual void Update(double humidity, double temp, double pressure) = 0;
  // See Strategy source code for comments
  virtual ~ObserverInterface() = 0 {};
};
 
class SubjectInterface{
public:
    virtual void RegisterObserver(ObserverInterface *o) = 0;
    virtual void RemoveObserver(ObserverInterface *o) = 0;
  virtual ~SubjectInterface() = 0 {};
 
private
    virtual void NotifyObservers(void) const = 0;
};
 
class DisplayElementInterface{
public:
    virtual void Display(void) const = 0;
  virtual ~DisplayElementInterface() = 0 {}
};
 
// ----------------------------------------------------------------------------
// Tracks the data coming from the weather station and updates the displays
// WeatherData is the concrete subject
class WeatherData : public SubjectInterface {
private:
    double mHumidity;
    double mTemperature;
    double mPressure;
    list<ObserverInterface*> mObservers;
 
public:
    void RegisterObserver(ObserverInterface *o) {
        mObservers.push_back(o);
    }
 
    void RemoveObserver(ObserverInterface *o) {
        mObservers.remove(o);
    }
 
    void NotifyObservers(void) const {
        for (auto o : mObservers){
            o->Update(mHumidity, mTemperature, mPressure);
        }
    }
 
  // In real life, it is supposed to be called by the weather station directly
    void MeasurementChanged(void){
        NotifyObservers();
    }
 
    // Helper-Fake function.
    // Everything looks like, data are updated by the weather station and then MeasurementChanged() gets called by the weather station
    void SetMeasurements(double Humidity, double Temperature, double Pressure){
        mHumidity           = Humidity;
        mTemperature    = Temperature;
        mPressure           = Pressure;
        MeasurementChanged();
    }
};
 
// ----------------------------------------------------------------------------
// Shows to the users the current weather conditions
// A concrete Observer and a concrete Display
class CurrentConditionsDisplay : public ObserverInterface, public DisplayElementInterface{
private:
    double  mHumidity;
    double  mTemperature;
    double  mPressure;
    SubjectInterface& mWeatherData; // Keep a ref to the Subject just in case. Not used here however
 
public:
    CurrentConditionsDisplay(SubjectInterface& weatherData) : mWeatherData(weatherData) {
        mWeatherData.RegisterObserver(this);
    }
 
    // Implementation from the ObserverInterface
    void Update(double Humidity, double Temperature, double Pressure) {
        mHumidity         = Humidity;
        mTemperature  = Temperature;
        mPressure         = Pressure;
        Display();
    }
 
    // Implementation from the DisplayElementInterface
    void Display(voidconst {
        cout << "Current Conditions Display :" << endl;
        cout << "\tHumidity            : " << mHumidity << endl;
        cout << "\tTemperature         : " << mTemperature << endl;
        cout << "\tPressure            : " << mPressure << endl;
        cout << endl;
    }
};
 
// ----------------------------------------------------------------------------
class AveragedConditionsDisplay : public ObserverInterface, public DisplayElementInterface{
private:
    vector<double>        mHumidity;
    vector<double>        mTemperature;
    vector<double>        mPressure;
    SubjectInterface& mWeatherData;
 
public:
    AveragedConditionsDisplay(SubjectInterface& weatherData) : mWeatherData(weatherData){
        mWeatherData.RegisterObserver(this);
    }
 
    void Update(double Humidity, double Temperature, double Pressure) {
        mHumidity.push_back(Humidity);
        mTemperature.push_back(Temperature);
        mPressure.push_back(Pressure);
        Display();
    }
 
    void Display(voidconst {
        cout << "Averaged Display :" << endl;
        cout << "\tAverage humidity    : " << accumulate(mHumidity.begin(), mHumidity.end(),0.0)/mHumidity.size() << endl;
        cout << "\tAverage temperature : " << accumulate(mTemperature.begin(), mTemperature.end(), 0.0) / mTemperature.size() << endl;
        cout << "\tAverage pressure    : " << accumulate(mPressure.begin(), mPressure.end(), 0.0) / mPressure.size() << endl;
        cout << endl;
    }
};
 
// ----------------------------------------------------------------------------
static void Test(void){
 
    WeatherData                             MyWeatherDataObject;
    CurrentConditionsDisplay    CurrentDisplay(MyWeatherDataObject);
    AveragedConditionsDisplay   StatisticDisplay(MyWeatherDataObject);
 
    // Simulate new weather measurements
    MyWeatherDataObject.SetMeasurements(80, 22, 1013);
    MyWeatherDataObject.SetMeasurements(85, 25, 1023);
    MyWeatherDataObject.SetMeasurements(90, 26, 1020);
    MyWeatherDataObject.SetMeasurements(85, 25, 1025);
 
  MyWeatherDataObject.RemoveObserver(&StatisticDisplay);
  cout << "Now we have one display." << endl << endl;
 
  MyWeatherDataObject.SetMeasurements(100, 40, 1000);
}
 
// ----------------------------------------------------------------------------
int main(){
#ifdef _MSC_VER
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
 
    Test(); // Putting all code in Test that way, allow to check memory leaks
 
#ifdef _MSC_VER
    _CrtMemDumpAllObjectsSince(NULL); // Begins the dump from the start of program execution
    _CrtDumpMemoryLeaks();
#endif
 
    cout << endl << "Strike ENTER to exit : ";
    cin.ignore((numeric_limits<streamsize>::max)(), '\n');
}

Prochaine étape ? Chapitre III et le Decorator pattern

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

  

  

  

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.