I’m implementing the Observer Pattern from Head First Design Patterns in C++ and I’m facing a little problem. I got the error : “class Subject has no member named setNewMeasurments ” on the line station->setNewMeasurments(0.5,10.0, 7.5);
It seem that if I want to use polymorphism, the method “setNewMeasurments
” should be defined in the interface (“Subject”). BUT this violates the principal of "Programming to Interfaces" and I really need polymorphism to make the Observer Pattern work here. Please any comments? Thank you!!!
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
/**************************************************
*************** Interfaces ******************
***************************************************/
class Observer
{
public:
virtual void update(float temp, float humidity, float pressure) = 0 ;
};
class Subject
{
protected:
vector<Observer*> observers; //est-ce une bonne idée ici? c'est contraire au pattern strategy (separate changing elements from constant ones) -> Visiblement c'est ce qui est fait dans l'UML page 56
public:
virtual void registerObserver(Observer* o) = 0;
virtual void removeObserver(Observer* o) = 0;
virtual void notifyObserver() = 0;
};
class DisplayElement
{
public:
virtual void display() const = 0 ;
};
/**************************************************
************* Implementations ***************
***************************************************/
class WeatherData : public Subject
{
private:
float temperature;
float humidity;
float pressure;
public:
virtual void registerObserver(Observer* o){
observers.push_back(o); //y'a as un probleme la?
}
virtual void removeObserver(Observer* o){
std::vector<Observer*>::iterator position=std::find(observers.begin(), observers.end(), o);
if (position != observers.end())
observers.erase(position);
}
virtual void notifyObserver()
{
for (vector<Observer*>::iterator it=observers.begin() ; it!=observers.end() ; it++)
{
(*it)->update(temperature, humidity, pressure);
}
}
void measurmentChanged() {
notifyObserver();
}
void setNewMeasurments(float temp, float hum, float press)
{
this->temperature=temp;
this->humidity=hum;
this->pressure=press;
measurmentChanged();
}
};
class CurrentConditionsDisplay : public Observer, public DisplayElement
{
private:
float temperature;
float humidity;
float pressure;
Subject* weatherData;
// Faut penser à désallouer cette mémoire dans le destructeur --> exercice!! (le faire à la main puis utiliser le Wrapper)
public:
CurrentConditionsDisplay(Subject* w){
weatherData=w;
weatherData->registerObserver(this);
}
void update(float temp, float hum, float press){
temperature=temp;
humidity=hum;
pressure=press;
display();
}
void display() const {
cout << "Current Conditions Displayers " << endl << endl;
cout << "Temperature : " << temperature << endl;
cout << "Humidity : " << temperature << endl;
cout << "Pressure : " << temperature << endl;
};
};
int main()
{
Subject* station=new WeatherData;
CurrentConditionsDisplay mDisp(station);
station->setNewMeasurments(0.5,10.0, 7.5);
delete station;
system("PAUSE");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire