JObservable

From Joomla! Documentation
Revision as of 05:23, 9 August 2009 by Batch1211 (Talk | contribs)

Jump to: navigation, search

JObservable is an abstract class which allows you to simply implement a Subject/Observer pattern within your application.

Contents

Availability

From Joomla 1.5 Joomla 1.6

Defined in

/libraries/joomla/base/observable.php

Extends

Methods

Method name Description
__construct Constructor
attach Attach an observer object
detach Detach an observer object
getState Get the state of the Observable object
notify Update each attached observer object and return an array of their return values

Importing

jimport( 'joomla.base.observable' );

What the Subject/Observer Pattern does for you

It is inevitable that objects of your application interact with each other. However, this ofter causes strong dependencies of the objects between each other, which reduces the reusability of the individual classes.

If the reason of these dependencies is the fact that one object has to be informed, once another objects changes it's state, the Subject/Observer Pattern can help, and eliminate these dependencies

Examples

Let's say, we want to integrate Error-Handling in our application, but we don't know yet, if we want the error to be display, logged in a file, stored to the database, or all of the above. Our goal is, to keep the object that raises the error independent of the objects that store the error message.

class MyError extends JObservable {
   public $msg = NULL;
 
   function raiseError($msg){
      $this->msg = $msg;
      //Notify all attached ErrorHandlers of the state change.
      $this->notify();
   }
}
 
//We now implement the Observers, thus the error handlers
class ErrorHandlerDisplay extends JObserver {
   function update(){
      echo $this->subject->msg;
   }
}
class ErrorHandlerFileStorage extends JObserver {
   function update(){
      error_log($this->subject->msg;);
   }
}
class ErrorHandlerDB extends JObserver {
   function update(){
      $db = JFactory::getDBO();
      $sql = "INSERT INTO #__myerrors (message) VALUES (".$db->quote($this->subject->msg).")";
      $db->setQuery($sql);
      $db->query();
   }
}
 
//Now we can use newly implemented MyError class to raise Errors.
$error = new MyError();
 
/* The constructor of the observers automatically attaches the observer to the subject
 * In our example that means that the constructor of the error handler automatically
 * attaches the handler to the MyError Object.
 */
new ErrorHandlerDisplay($error);
new ErrorHandlerFileStorate($error);
new ErrorHandlerDB($error);
 
$error->raiseError('Help!!!');
 
/*
 * Would cause 'Help!!!' to be display, logged in a file, and stored in the database.
 * You can simply add and remove the error handlers as you like
 */

What happened here? We separated the functionality of raising an error of the functionality of handling an error. In the future you can add additional ErrorHandlers, or remove some of the existing handlers, without the need to change any classes at all. Furthermore you can simply change an Errorhandler, without the need to change the MyError class. This creatly increases the reusability of your code.


See also

Personal tools
Namespaces

Variants
Actions
Navigation
Joomla! Sites
Toolbox