Sw4   >   Misc   >   Observers

Observers

The section explains how to implement an observer using the oObserver class.

Observers are great for loose coupling of different subwindow instances. Suppose you are viewing a record in one subwindow and if the user changes to a different record you want another subwindow to be notifed when ever this happens. Observers are your friend for setting this up.

oObserver Class

The oObserver object class in swBase4 makes it very easy for you to set up and observer. There is also an oEventsObserver class which is a subclass of oObserver that specialized in notifying observers of event handling event.

To setup an observer you need to have an instance of oObserver inside the class where the change that you want to track will occur.

  1. Add an appropriately named ivar to the class. e.g. ioChangeRecordObserver
  2. Set the type to Object and point it to swBase4.oObserver
  3. Right-click ioChangeRecordObserver and select Interface Manager...
  4. Note the following public methods:
    • $attach - Use to attach an observer. (Generally I only have one observer, but the object will support up to 400 observers) Another name which could used for this method is $subscribe
    • $remove - Used to detach an observer (I rarely use this method) Another name which could used for this method is $unsubscribe
    • $notify - Use this method to notify all the attached observer when the things they have subscribed to observer has happened. Another name which could used for this method is $publish

Attach Observer

In order to attach an observer, we need to add an accessor method to the window class methods. Observers will use this method to subscribe for notifications when the thing they want to know about happens.

  1. Add an appropriately named class method. e.g. $attachChangeRecordObserver with the parameters pfrObserver - field reference and pCallBackMethod - character.
  2. This method forwards the call to ioChangeRecordObserver

    ; Forward the message to the observer class instance.
    Do ioChangeRecordObserver.$attach(pfrObserver,pCallBackMethod) Returns FlagOK
    Quit method FlagOK

  3. Other window instances can now attach themselves as observers. Or you could have a container window subscribe one of its subwindows to observe events in another subwindow.

    ; Subscribe the list subwin to be notified when ever the edit window changes its record.
    Do rEditSubWin.$attachChangeRecordObserver(rListSubWin,'$updateEditRecordChanged') Returns FlagOK
    Quit method FlagOK

  4. Make sure the observer instance has the callback method which was specified when it subscribed.
The great flexibility in the observer design pattern is that the call back method can be whatever you want it to be and you can change it without the observed class knowing or needing to change anything. Without the observer design pattern you would have to hard code the exact method calls on both sides.

Notify Observers

Finally, we need to notify the observers when the thing they are asking to be told about happens.

  1. Inside the class which has the ioChangeRecordObserver ivar you will need to add code in methods or event where the thing beings observed will happen.
  2. When the big moment happens, you simply send a $notify message to the the oObserver instance with parameter(s) that will be useful to the subscribers.

    ; Notify the observers.
    Do ioChangeRecordObserver.$notify(iList) Returns FlagOK
    Quit method FlagOK

That is all there is too it. Any attached observer will be notified through the call back method which the observer specified when it subscribed.