observer.h #ifndef OBSERVE_H #define OBSERVE_H #include "iostream" using namespace std; class Observer { public: Observer(); virtual void update(int hour, int min, int sec) = 0; }; #endif // OBSERVE_H
observer.cpp #include "observer.h" Observer::Observer() { }
subject.h #ifndef SUBJECT_H #define SUBJECT_H #include <list> #include "observer.h" using namespace std; class Subject { public: Subject(); void registerObserver(Observer *ob); void removeObserver(Observer *ob); virtual void notify() = 0; protected: list<Observer *> lo; }; #endif // SUBJECT_H
subject.cpp #include "subject.h" Subject::Subject() { } void Subject::registerObserver(Observer *ob) { lo.push_back(ob); } void Subject::removeObserver(Observer *ob) { lo.remove(ob); }
bejingtime.h #ifndef BEIJINGTIME_H #define BEIJINGTIME_H #include "subject.h" class BeijingTime : public Subject { public: BeijingTime(); void setTime(int hour, int min, int sec); void notify(); void dis() { cout << "hour:" << _hour << "min:" << _min << "sec:" << _sec << endl; } protected: int _hour; int _min; int _sec; }; #endif // BEIJINGTIME_H
beijingtime.cpp #include "beijingtime.h" BeijingTime::BeijingTime() { } void BeijingTime::notify() { list<Observer *>::iterator itr; for(itr = lo.begin(); itr != lo.end(); ++itr) { (*itr)->update(_hour, _min, _sec); } } void BeijingTime::setTime(int hour, int min, int sec) { _hour = hour; _min = min; _sec = sec; notify(); }
canadatime.h #ifndef CANADATIME_H #define CANADATIME_H #include "observer.h" class CanadaTime : public Observer { public: CanadaTime(); void update(int hour, int min, int sec); void dis() { cout << "hour:" << _hour << "min:" << _min << "sec:" << _sec << endl; } private: int _hour; int _min; int _sec; }; #endif // CANADATIME_H
canadatime.cpp #include "canadatime.h" CanadaTime::CanadaTime() { } void CanadaTime::update(int hour, int min, int sec) { _hour = hour + 100; _min = min; _sec = sec; }
londontime.h #ifndef LONDONTIME_H #define LONDONTIME_H #include "observer.h" class LondonTime : public Observer { public: LondonTime(); void update(int hour, int min, int sec); void dis() { cout << "hour:" << _hour << "min:" << _min << "sec:" << _sec << endl; } private: int _hour; int _min; int _sec; }; #endif // LONDONTIME_H
londontime.cpp #include "londontime.h" LondonTime::LondonTime() { } void LondonTime::update(int hour, int min, int sec) { _hour = hour + 200; _min = min; _sec = sec; }
tokyotime.h #ifndef TOKYOTIME_H #define TOKYOTIME_H #include "observer.h" class TokyoTime : public Observer { public: TokyoTime(); void update(int hour, int min, int sec); void dis() { cout << "hour:" << _hour << "min:" << _min << "sec:" << _sec << endl; } private: int _hour; int _min; int _sec; }; #endif // TOKYOTIME_H
tokyotime.cpp #include "tokyotime.h" TokyoTime::TokyoTime() { } void TokyoTime::update(int hour, int min, int sec) { _hour = hour + 300; _min = min; _sec = sec; }
main.cpp #include <iostream> #include "tokyotime.h" #include "londontime.h" #include "canadatime.h" #include "beijingtime.h" using namespace std; int main(int argc, char *argv[]) { TokyoTime tt; LondonTime lt; CanadaTime ct; BeijingTime bt; bt.registerObserver(&tt); bt.registerObserver(<); bt.registerObserver(&ct); bt.setTime(100, 200, 300); bt.dis(); tt.dis(); lt.dis(); ct.dis(); return 0; }