StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SubjectObserver.h
1 //SubjectObserver.h
2 //M.L. Miller (Yale Software)
3 //11/01
4 
5 #ifndef SubjectObserver_HH
6 #define SubjectObserver_HH
7 #include "Stiostream.h"
8 #include <vector>
9 using std::vector;
10 
11 #include <algorithm>
12 using std::for_each;
13 
14 class Subject;
15 class Observer;
16 
17 class Subject
18 {
19 public:
21  typedef vector<Observer*> ObserverVec;
22 
23  //ctr-dstr
24  Subject();
25  virtual ~Subject();
26 
27  //gets/sets
28  virtual void attach(Observer*);
29  virtual void detach(Observer*);
30 
31  //Action
32  virtual void notify();
33 
34 protected:
35  ObserverVec mObservers;
36 };
37 
38 class Observer
39 {
40 public:
41 
42  Observer();
43  Observer(Subject * subject);
44  virtual ~Observer();
45  virtual void getNewState()=0;
46  virtual void changed(Subject* changedSubject);
47  virtual void forgetSubject(Subject* theObsoleteSubject);
48 
49 protected:
50 
51  Subject* mSubject;
52 };
53 
54 /*
55  inline void Observer::changed(Subject* changedSubject)
56  {
57  if (changedSubject!=mSubject)
58  cout <<"Observer::changed(Subject*) - ERROR - changedSubject!=mSubject"<<endl;
59  else
60  getNewState();
61  }*/
62 
63 inline void Observer::forgetSubject(Subject* obsolete)
64 {
65  if (obsolete==mSubject)
66  mSubject=0;
67  else
68  cout <<"Observer::forgetSubject(Subject*) - ERROR - obsolete!=mSubject"<<endl;
69 }
70 
71 #endif
vector< Observer * > ObserverVec
For convenience.