Changes between Version 1 and Version 2 of ObserverHowto


Ignore:
Timestamp:
Sep 13, 2012, 1:58:53 PM (12 years ago)
Author:
ankele
Comment:

Observable's constructor missed channel creation

Legend:

Unmodified
Added
Removed
Modified
  • ObserverHowto

    v1 v2  
    33== Theory ==
    44
    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 observables in turn have to sign on to the observer's list.
     5The ''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.
    66
    77== Implementation ==
     
    1111  * derive the observable class from {{{Observable}}}
    1212  * 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)
    1315  * surround any code that makes notify-worthy changes with {{{OBSERVE;}}} and {{{NOTIFY(channel);}}} with {{{channel}}} the appropriate enum constant
    1416
     
    1820  class MyObservable : public Observable
    1921  {
    20     ...
    2122  public:
    2223    enum NotificationType{ // should be public (the observer will want to use this type!)
    2324      SomethingDone,
    24       OtherStuffDone
     25      OtherStuffDone,
     26      NotificationType_MAX
    2527    };
     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    ....
    2639    void doSomeChanges()
    2740    {