2010年1月19日星期二

RE: [fw-mvc] Re: [fw-general] Bootstrap Resource usage in model - best practice advice please

Smth like that wolud be nice :)
And yes, there should be no more than one cache instance (at least 1 backend for cache).


class Default_Model_Base_Abstract
{
protected static $_cache;

protected function _getCacheInstance()
{
if (null === self::$_cache)
self::$_cache = Zend_Registry::get('Cache'); //or factory

return self::$_cache;
}

}


Alvar

-----Original Message-----
From: Andreas Möller [mailto:localheinz@l8m.de]
Sent: Tuesday, January 19, 2010 7:36 PM
To: fw-mvc
Subject: Re: [fw-mvc] Re: [fw-general] Bootstrap Resource usage in model - best practice advice please

> Zend_Registry uses Martin Fowler's Registry pattern. Not singleton.

Even if it does, it still follows the Singleton pattern, too. Or how is
the Zend_Registry instance accessed?

> No, it's not. Passing object by reference doesn't mean singleton.

Ok. But the point is, that a Singleton pattern is mimicked with the
example that was previously made, i.e.

class Default_Model_Base_Abstract
{

protected $_cacheKey = 'opamama';

protected function _getCacheInstance()
{
if (Zend_Registry::isRegistered($this->_cacheKey)) {
$cache = Zend_Registry::get('cache');
} else {
$cache = Zend_Cache::factory();
Zend_Registry::set($this->_cacheKey, $cache);
}
return $cache;
}

}

> You're wrong. As you know, objects since PHP5 are passed by reference.
> So you will have most probably only one object instance, referenced to
> each model, except you prefer otherwise. And that's the way I would prefer.

So, how are you going to access and store your cache instance?


Best regards,

Andreas

没有评论: