Skip to main content

PHP Registry design pattern

Registry Pattern


What if we could treat ordinary classes as limited-instance classes, without having to make them Singletons? The Registry is a Singleton, used to store instance of other “normal” classes. Another way to think of it is as a global key/instance storeroom. Instances of non-Singleton classes are given a key (identifier) and are “kept” inside the Registry’s private storage. When requests are made for specific keys, the Registry checks whether there are any instances for those keys, and returns them. If no instance is found at a key, the Registry’s methods will return a default value.
You might be wondering what practical use this is to our framework. There are many times when we will need to limit the amount of class instances, as they relate to shared connections to third-party services or shared resources. Consider our cache system. For most applications, one cache provider will be sufficient. You will, for instance, connect to one Memcached server. We certainly don’t want to be making multiple connections to the same service when one connection will suffice.
We could build our cache system as a set of Singleton classes, or we could declare multiple instances of the Cache classes. Using Registry, we can find a neat, efficient middle ground that allows us strict control over the number of class instances, while not limiting us to single-instance use only.
Consider an application that uses a database, or one that connects to Facebook. Both of these cases would benefit from strict control and resource sharing. Registry accommodates those needs, without requiring us to write a multitude of singletons.
Let’s look at some code in Listing 6-4.

Listing 6-4. The Registry Class

namespace Framework
{
class Registry
{
private static $_instances = array();
private function __construct()
{
// do nothing
}
private function __clone()
{
// do nothing
}
public static function get($key, $default = null)
{
if (isset(self::$_instances[$key]))
{
return self::$_instances[$key];
}
return $default;
}
public static function set($key, $instance = null)
{
self::$_instances[$key] = $instance;
}
public static function erase($key)
{
unset(self::$_instances[$key]);
}
}
}

?>
I referred to the registry as a singleton, but it is actually a static class. Static classes are singletons that have no instance properties/methods or instance accessors/mutators. In fact, the only difference between a static class and our Registry is that no instance of our registry can ever be created. This is fine because we will only need our Registry class in a single context.
The set() method is used to “store” an instance with a specified key in the registry’s private storage.
The get() method searches the private storage for an instance with a matching key. If it finds an instance, it will return it, or default to the value supplied with the $default parameter. The erase() method is useful for removing an instance at a certain key.
The Registry class is easy to use with our Ford/Car classes, as demonstrated in Listing 6-5.
Listing 6-5. Using Registry

Framework\Registry::set("ford", new Ford());
$car = new Car();
$car-> setColor("Blue")-> setProducer(Framework\Registry::get("ford"));
echo Framework\Registry::get("ford")-> produces($car);
echo Framework\Registry::get("ford")-> founder;

■■Note Zend Framework and CakePHP both have classes dedicated to the organization of object
instances—classes comparable to Registry. CodeIgniter has no such class, and depends solely on the use of the Singleton pattern.


By (chayon shaah)

Comments

  1. Those Books I shared for Learning . In slideshare i shared my collection .. So why you have come here with slideshare information .. That is sharing site and I am sharring ..

    ReplyDelete

Post a Comment