JObservable/1.5
From Joomla! Documentation
Description:
Abstract observable class to implement the observer design pattern. JObservable is an abstract class which allows you to simply implement a Observer/Observable pattern within an application.
The JObservable class can be used where one or more objects need to be informed when the state of another object changes. In order to avoid strong dependencies between objects, which reduces reusability of the individual classes, it is common to use the Observer/Observable design pattern. Objects derived from the JObserver class can register themselves with an object derived from the JObservable class and will be informed as required.
Inheritance for JObservable:
Level 1.jObject
Level 2. jObservable
Level 3. jAuthontication / jDispatcher / jEditor
jObject: Object class, allowing __construct in PHP4.
jObservable: Abstract observable class to implement the observer design pattern
jAuthontication: Authenthication class, provides an interface for the Joomla authentication system
jDispatcher: jDispatcher Class to handle dispatching of events.
jEditor: JEditor class to handle WYSIWYG editors
jObservable defined in libraries/joomla/base/observable.php
Example:
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 */