Singleton / SessionSingleton

The singleton pattern is a widely used pattern. It belongs to the family of producing 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 better 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:

PHP code
$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

PHP code
import('core::singleton','SessionSingleton');

Further any object can be created in session singleton style by adding

PHP code
$object = &SessionSingleton::getInstance('{ClassName}');

to your code.

3. Usage of the ServiceManager

As already mentioned above, every class, that inherits from APFObject 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 following code part can be used to create service objects:

PHP code
$service = &$this->getServiceObject('service::layer::namespace', 'ServiceLayerClassName'[, '{MODE}']);

{MODE} can 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"

In case {MODE} is not applied, the service object is created in SINGLETON mode.

3.2. getAndInitServiceObject()

In order to create an instance using the getAndInitServiceObject() method, the desired class must inherit from APFObject 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

PHP code
class MyServiceLayer extends APFObject { private $mySpecialParam = null; public function init($initParam){ $this->mySpecialParam = $initParam; } }

using the method getAndInitServiceObject(), the following lines must be placed in you application's code:

PHP code
$service = &$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.