As a result, care should be taken when designing a class to be a singleton and also when choosing to use
the getInstance() function elsewhere in the code. Usually, it is better to pass an object around so that it is easier
to control access to it than to access the Singleton directly. An example of where we can do this is within the
action controller class: Zend_Controller_Action provides a member variable _request which is a reference
to the Request object and this member variable should be used in preference to
Zend_Controller_Front::getInstance()->getRequest().
Having said that, it is not uncommon to need to access commonly used information from multiple places,
for example configuration data. To help control this, the Registry pattern provides the tool to ensure that
managing such data works well.
17.4.2 The Registry pattern
The registry pattern allows us to take any object and treat it as a Singleton and also allows us to centrally
manage an object. The biggest improvement over the Singleton is that is possible to have two instances of an
object if required. A typical scenario is a database connection object. Usually, you connect to one database for
the duration of the request and so it is tempting to make the object as a Singleton. However, you may need to
connect to a second database every so often, such as for an export or import and for those times only you need
two instances of the database connection object.
Pages:
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379