Getting Started - part 2 - JObserver and JObservable
Posted: Sun Aug 19, 2007 2:15 am
Let´s continue our Base subpackage review.
We already saw JObject class defined in object.php, at Getting Started - JObject thread.
Now we´ll see JObserver class and JObservable class, both defined in observer.php
For this, one must study and understand well the Observer Pattern, because JObserver and JObservable implements this programming pattern.
[quote=∓quot;Wikipedia"\]Observer is a publish/subscribe pattern which allows a number of observer objects to see an event.[/quote]
Please read the Observer Pattern article at Wikipedia for more information.
(Another reference, that I haven´t read: Observer Pattern article at phpPatterns)
I think it is easy to understand the purpose of the Observer Pattern with the following example:
In this example, Clock is descendant of the JObservable class, while AnalogClock and DigitalClock are descendants of the JObserver class.
$clock is the object that measures the time (the "quartzo"): it is the observable. Both $analogClock and $digitalClock are objects that display, in different ways, the time measured by $clock: they are the observers!
The JObserver and JObservable classes are the BASE for the Event Handler and Plug In mechanism that Joomla! has.
Event Handling concept should be well-known for most developers. The Observer Pattern is the programming pattern used behind an Event Handling mechanism.
Some references:
http://en.wikipedia.org/wiki/Event-driven_programming
http://en.wikipedia.org/wiki/Event_handler
We already saw JObject class defined in object.php, at Getting Started - JObject thread.
Now we´ll see JObserver class and JObservable class, both defined in observer.php
For this, one must study and understand well the Observer Pattern, because JObserver and JObservable implements this programming pattern.
[quote=∓quot;Wikipedia"\]Observer is a publish/subscribe pattern which allows a number of observer objects to see an event.[/quote]
Please read the Observer Pattern article at Wikipedia for more information.
(Another reference, that I haven´t read: Observer Pattern article at phpPatterns)
I think it is easy to understand the purpose of the Observer Pattern with the following example:
Code: Select all
<?php
$clock = new Clock;
$analogClock = new AnalogClock($clock);
$digitalClock = new DigitalClock($clock);
?>
In this example, Clock is descendant of the JObservable class, while AnalogClock and DigitalClock are descendants of the JObserver class.
$clock is the object that measures the time (the "quartzo"): it is the observable. Both $analogClock and $digitalClock are objects that display, in different ways, the time measured by $clock: they are the observers!
The JObserver and JObservable classes are the BASE for the Event Handler and Plug In mechanism that Joomla! has.
Event Handling concept should be well-known for most developers. The Observer Pattern is the programming pattern used behind an Event Handling mechanism.
Some references:
http://en.wikipedia.org/wiki/Event-driven_programming
http://en.wikipedia.org/wiki/Event_handler