Quicknavi |
|
Class reference table - Singleton / SessionSingleton
The singleton pattern is a widly used pattern. It belongs to the family of creational patterns and
is often used to to make available information or functionality just once within a entire
application. In case of software layering, the singleton pattern commonly is used to create service
layers only once to get bettern performance during executing business processes or load and save
data structures.
The adventure php framework implements two special ways of the design pattern: an abstract singleton
class and an abstract session singleton class. Both of them offer the ability to create any
class in singleton style. The singleton pattern is also used in the serviceManager component
due to performance reasons. The session singleton implementation creates singleton objects, that are
available within a (PHP) session in singleton style.
1. Singleton
Due to the fact, that the singleton pattern is deeply interweaved with the framework, this class must
not be imported explicitly. To create any class in singleton style, every time, the class must fetched
using the following code snippet:
$Object = &Singleton::getInstance('{ClassName}');
If your application is based on the PHP 5 version you can leave the &, because
values are automatically returned by reference.
2. SessionSingleton
In order to utilize the SessionSingleton class, it must first be imported via
import('core::singleton','SessionSingleton');
Further any object can be created in session singleton style by adding
$Objekt = &SessionSingleton::getInstance('{ClassName}');
to your code. Please note, that PHP 4 users must add the &.
3. Usage of the serviceManagers
As already mentioned above, every class, that inherits from coreObject features
wrapper methods for creating singleton and of version 1.5.1 creating session singleton service
layers. You are provided two different possibilities: simple service objects and pre-initialized
objects. Generally, standard singleton objects meet the needs of the application. In more complex
applications the service layers often need to be initialized prior to use.
Since version 1.5.1 both methods are provided with another optional argument, that indicates the
way, the service object is created.
A "service object" or "service layer" commonly is an instance of any kind of class, that provides
a kind of service to another software layer. Here, software layer is a notion, that is described by
the three tier architecture pattern. In case of a business layer communicating
with the data layer, the latter is called a "service layer" for the business layer.
3.1. __getServiceObject()
Merely, the follwing code part can be used ton create service objects:
$SL = &$this->__getServiceObject('service::layer::namespace','ServiceLayerClassName','{MODE}');
{MODE} must be filled with on of the values presented in the list below:
- NORMAL: Class will be created and configured "normally"
- SINGLETON: Class will be created and configured "singleton"
- SESSIONSINGLETON: Class will be created and configured "session singleton"
3.2. __getAndInitServiceObject()
In order to create an instance using the __getAndInitServiceObject() method, the
desired class must inherit from coreObject and implement the abstract
init() function. The init() method is designed to initialize the
class using the initialization parameter taken as an argument of init(). The
parameter can be any kind of data type. Usually strings or arrays are used. If you intend to create
an instance of the class
class myServiceLayer extends coreObject {
var $__MySpecialParam = null;
function myServiceLayer(){ }
function init($InitParam){ $this->__MySpecialParam = $InitParam; // end function }
// end class }
using the method __getAndInitServiceObject(), the follwing lines must be placed in
you application's code:
$SL = &$this->__getAndInitServiceObject('service::layer::namespace','myServiceLayer','InitParamValue','{MODE}');
InitParamValue is to be replaced with the configuration value of your choice.
{MODE} must be filled with on of the values presented in the list below:
- NORMAL: Class will be created and configured "normally"
- SINGLETON: Class will be created and configured "singleton"
- SESSIONSINGLETON: Class will be created and configured "session singleton"
Comments
Do you want to add a comment to the article above, or do you want to post additional hints? So please click here. Comments already posted can be found below.
There are no comments belonging to this article.
|