WHAT'S HOT
Prev | Current Page 373 | Next

Rob Allen, Nick Lo, and Steven Brown

"Zend Framework in Action"

Let??™s look at a couple of the common patterns to see how it works in
practice.
17.4.1 The Singleton pattern
The Singleton design pattern is a very simple pattern that is intended to ensure that only one instance of an
object may exist. This is used by the Zend Framework??™s Zend_Controller_Front front controller to ensure that
there is only one front controller in existence for the duration of the request. Let??™s look at the code:
Listing 17.14 The Singleton as implemented by Zend_Controller_Front
class Zend_Controller_Front
{
protected static $_instance = null; 1
private function __construct() 2
{
$this->_plugins = new Zend_Controller_Plugin_Broker();
}
public static function getInstance() 3
{
if (null === self::$_instance) { 4
self::$_instance = new self(); 4
} 4
return self::$_instance; 5
}
}
1 Static variable holds the one instance of this object
2 private constructor
3 method to allow creation
4 create one instance if not already created
5 return instance to user
There are three pieces in the Singleton jigsaw as implemented by Zend_Controller_Front, although every
other implementation is similar. A static variable is used to store the instance of the front controller (#1). This
is protected to prevent anyone outside of this class (or its descendants) from accessing it directly.


Pages:
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
pustaki szklane House Extension Kąty Rybackie noclegi kasyno Kołobrzeg