CHAPTER 12 ■ ENTERPRISE PATTERNS
The base class defines two protected methods, get() and set(). They are not available to client
code, because I want to enforce type for get and set operations. The base class may define other public
methods such as isEmpty(), isPopulated(), and clear(), but I’ll leave those as an exercise for you to do.
■Note In a real-world system, you might want to extend this structure to include another layer of inheritance.
You might keep the concrete get() and set() methods in their respective implementations, but specialize the
public getAaa() and setAaa() methods into domain-specific classes. The new specializations would become the
singletons. That way you could reuse the core save and retrieve operations across multiple applications.
Here is the abstract class as code:
namespace woo\base;
abstract class Registry {
abstract protected function get( $key );
abstract protected function set( $key, $val );
}
■Note Notice that I’m using namespaces in these examples. Because I will be building a complete, if basic,
system in this chapter, it makes sense to use a package hierarchy, and to take advantage of the brevity and clarity
that namespaces can bring to a project.
The request level class is pretty straightforward. In another variation from my previous example, I
keep the Registry sole instance hidden and provide static methods to set and get objects. Apart from
that, it’s simply a matter of maintaining an associative array.
namespace woo\base;
// ...
class RequestRegistry extends Registry {
private $values = array();
private static $instance;
private function __construct() {}
static function instance() {
if (! isset(self::$instance) ) { self::$instance = new self(); }
return self::$instance;
}
protected function get( $key ) {