zondag 7 maart 2010

Observer Pattern

Handles the relation between a Publisher and his subscribers (or Subject/observers or Observable/observers).

How
  • The Observer-interface: All observers must implement this interface: So they all have a Notify-method.
  • The observable base-class: Keeps track of all the observer-objects (reference to their Observer-interface)
  • Your observable class will inherit from the observable baseclass. This class will thus inherit the behaviour that manages and notifies the observers. When needed, you just call NotifyObservers().
  • Your observers will implement the Observer-interface, so their Notify-method can be called by the Observable class.
To pass the new data between the observable and the observers:
  1. Pass an object with all the data via the Notify-methods
  2. The observer gets the data via the properties of the observable-object
Observable calls Notify() on an interface => Loosely coupled.

Geen opmerkingen: