Changes between Version 1 and Version 2 of ObserverHowto
- Timestamp:
- Sep 13, 2012, 1:58:53 PM (12 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ObserverHowto
v1 v2 3 3 == Theory == 4 4 5 The ''Observer'' pattern is a way of implementing a one-to-many dependency. There usually is one observable object automatically notifying multiple observer objects of its changes. For doing so the observer keeps a list of its observables. The observ ables in turn have to sign on to the observer's list.5 The ''Observer'' pattern is a way of implementing a one-to-many dependency. There usually is one observable object automatically notifying multiple observer objects of its changes. For doing so the observer keeps a list of its observables. The observers in turn have to sign on to the observable's list. 6 6 7 7 == Implementation == … … 11 11 * derive the observable class from {{{Observable}}} 12 12 * create an enum type with one element for each type of change about to occur 13 * in the constructor call the {{{Observable}}} constructor giving a string name for your class 14 * also in the constructor create the channels (see code below) 13 15 * surround any code that makes notify-worthy changes with {{{OBSERVE;}}} and {{{NOTIFY(channel);}}} with {{{channel}}} the appropriate enum constant 14 16 … … 18 20 class MyObservable : public Observable 19 21 { 20 ...21 22 public: 22 23 enum NotificationType{ // should be public (the observer will want to use this type!) 23 24 SomethingDone, 24 OtherStuffDone 25 OtherStuffDone, 26 NotificationType_MAX 25 27 }; 28 MyObservable() : Observable("MyObservable") 29 { 30 Channels *OurChannel = new Channels; 31 NotificationChannels.insert( std::make_pair(this, OurChannel) ); 32 // add instance for each notification type 33 for (size_t type = 0; type < NotificationType_MAX; ++type) 34 OurChannel->addChannel(type); 35 // other constructor stuff 36 ... 37 } 38 .... 26 39 void doSomeChanges() 27 40 {