• home
  • forum
  • my
  • kt
  • download
  • Observer Pattern

    Author: 2009-03-09 15:02:14 From:

    Observer Pattern: When objects have a one to many relationship, i.e. many objects need to be updated when one object changes, and the number of these objects is not known ahead of time, this pattern can be used to implement it.

    We begin by implementing 2 interfaces:
    • IObservable: This is the object that gets observed - whose change would affect other objects
    • IObserver: This represents objects that are the observers
    Here is a pseudo-code implementation of the interfaces:

    Interface IObservable {
    // The attachObserver function adds the passed object to an array
    public function attachObserver(obj);
    // The detachObserver function removes the passed object from the array
    public function detachObserver(obj);
    // Notify all the objects in the array - loop through the objects and call the update method
    // Note: All the elements in the array implement the IObserver interface and hence
    // support the "update" method
    public function notify();
    // Function would be called by the Observers to get state information about the object
    public function getState();
    }

    Interface IObserver {
    // The method that performs the action that needs to be done on change of the observable
    public function update();
    }

    The actual observer and observable classes would implement these interfaces.

    Two examples of where you might be using this:
    • In a web application, you may register screens with your business model objects (representing underlying data) - hence when the model gets updated, the screens get updated automatically.
    • Another example may be, say you are making a game - and you when 1 object performs a task - such as a commander issuing a fire order, all units within the commanders range should fire (they would have registered with the commander object - and as they observed a change, they perform an action).
    We have covered what the observer pattern is, how it is coded, and two examples of where it may be used. The answer for the why is the same as why use design patterns (see the post on Design Patterns in general). With that I will end this post. I hope someone, someday, somewhere (may be myself) will find this useful. :)

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (41)
      Cookies and Sessions (12)
      Counters (15)
      Database Related (34)
      Date and Time (15)
      Development (22)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (14)
      Error Handling (8)
      File Manipulation (36)
      Flash and PHP (6)
      Form Processing (22)
      Guestbooks (12)
      Image Manipulation (26)
      Installing PHP (7)
      Introduction to PHP (29)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (60)
      Networking (9)
      News Publishing (9)
      OOP (26)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot