JObservable: Difference between revisions
From Joomla! Documentation
New page: <span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>Edit Descripton<nowiki>]</nowiki>
</span>
{{Description:JObservable}}
===Defined in==... |
m preparing for archive only |
||
| (3 intermediate revisions by 3 users not shown) | |||
| Line 1: | Line 1: | ||
JObservable is an abstract class which allows you to simply implement a Observer/Observable pattern within an application. | |||
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. The JObservable class can be used where one or more objects need to be informed when the state of another object changes. Objects derived from the JObserver class can register themselves with an object derived from the JObservable class and will be informed as required. | |||
===Defined in=== | ===Defined in=== | ||
| Line 27: | Line 26: | ||
|Detach an observer object | |Detach an observer object | ||
|} | |} | ||
===Importing=== | ===Importing=== | ||
<source lang="php">jimport( 'joomla.base.observable' );</source> | <source lang="php">jimport( 'joomla.base.observable' );</source> | ||
===Examples=== | ===Examples=== | ||
=== Code Examples === | |||
<dpl> | <dpl> | ||
noresultsheader=\n | noresultsheader=\n | ||
namespace=CodeExample | |||
category=JObservable | category=JObservable | ||
namespace=CodeExample | |||
category=ClassExample | category=ClassExample | ||
include=* | include=* | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
Latest revision as of 00:59, 25 March 2017
JObservable is an abstract class which allows you to simply implement a Observer/Observable pattern within an application.
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. The JObservable class can be used where one or more objects need to be informed when the state of another object changes. Objects derived from the JObserver class can register themselves with an object derived from the JObservable class and will be informed as required.
Defined in
libraries/joomla/base/observable.php
Methods
| Method name | Description |
|---|---|
| __construct | Constructor |
| getState | Get the state of the JObservable object |
| notify | Update each attached observer object and return an array of their return values |
| attach | Attach an observer object |
| detach | Detach an observer object |
Importing
jimport( 'joomla.base.observable' );
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.
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 greatly increases the reusability of your code.
This example was originally provided by Batch1211.