Page 1 of 1
Registry Pattern
Posted: Fri Sep 23, 2005 8:42 am
by feyd
Although I've found lots of vague descriptions of what a Registry Pattern is and does (Fowler didn't help much in PoEEA), I can't quite figure out how to go about creating the interface and/or implementation.. it seems to me that it's just a named property bucket..

Here's the really rough idea of what I'm seeing one as:
Code: Select all
class TRegistry {
private $FRegistry;
function __construct() {
// TODO add constructor logic, if any
$this->$FRegistry = array();
}
function __destroy() {
// TODO add destruction logic, if any
}
function AddItem($name,$value) {
while(isset($this->FRegistry[$name])) {
$name = uniq_id();
}
$this->FRegistry[$name] = $value;
return $name;
}
}
overload the __get and probably __set to make it "easier"... would have to overload __call too

damn calling methods.. oh well..
Posted: Fri Sep 23, 2005 10:00 am
by dbevfat
yes, it's basically a "named property bucket", but you can specialize it into a "specifical properties bucket" by getting rid of more general Get/Set in favour of more specific getters/setters, like GetDatabase/SetDatabase, GetUser/SetUser ...
Also, you can put more specialization to it, like denying writing to properties that were already set (allow first write only).
But you must store them in such manner that you can read them afterwards, so using uniq_id() is not the way to go. You'd be better off storing them in a hash with their name as the index, this way the reading would work, too.
Code: Select all
function get($name)
{
if (isset($this->FRegistry[$name]))
return $this->FRegistry[$name];
else
return null;
}
function set($name, $value)
{
$this->FRegistry[$name] = $value;
}
edit + offtopic: i see you use pascal (delphi) naming conventions, noticed the T for class and F for private member preffix.

Posted: Fri Sep 23, 2005 10:04 am
by feyd
hmm okay.. it was just bizarre that its talked about all over (sorta) but no one could seem to hammer down an actual interface for me to understand
So now I see it as a theory pattern and not so much an actual finite pattern.. it'd be nice if someone would say that.

Posted: Fri Sep 23, 2005 11:53 am
by Maugrim_The_Reaper
it seems to me that it's just a named property bucket..
Aha...

This must be one of the worst described patterns ever...its a good runner. I'm not even sure it's always put in the right context when described. Last implementation I saw was basically storage for singletons...
Posted: Fri Sep 23, 2005 2:15 pm
by Jenk
So this pretty much just makes a unique variable for you to use? (I've not been in a position to need to know this stuff, so I am asking as I am learning

)
Posted: Sun Sep 25, 2005 5:07 pm
by McGruff
feyd wrote:hammer down an actual interface for me to understand
With a singleton Registry you get easy global access to registry properties:
Code: Select all
class Foo
{
function Foo()
{
$registry =& Registry::getInstance();
$this->quark = $registry->getQuark();
$this->strangeness = $registry->getStrangeness();
$this->charm = $registry->getCharm();
// etc
You don't have to use named getters although I think I'd prefer that myself.
As Fowler says "global data is always guilty until proven innocent" so you'd probably want to hold off until the pressure mounts ie passing stuff around a lot through methods which are merely handing over to other methods, & etc. A singleton registry can be used as a
ServiceLocator.
Also note the
static reference problem.
Unfortunately phppatterns.com is down right now (I think there are plans either to restore the site or to get the content up somewhere else). Marcus Baker had an article on the Registry pattern here, including a version adapted to allow testing - obviously it's hard to create and destroy multiple instances of a singleton in a single test script. I'm afraid I can't remember how he did it.
It kind of feels like OOP's dirty little secret doesn't it? The whole point of encapsulation is to restrict scope. At least it gives you a bit more control than simply using global vars.
Posted: Sun Sep 25, 2005 6:36 pm
by feyd
phppatterns.com is back up!

Posted: Sun Sep 25, 2005 7:36 pm
by McGruff
Great!
Posted: Mon Sep 26, 2005 7:46 pm
by Ambush Commander
Are you sure? I couldn't reach it...
Posted: Mon Sep 26, 2005 7:49 pm
by feyd
it was up yesterday

Posted: Mon Sep 26, 2005 8:34 pm
by Jenk
Jenk wrote:So this pretty much just makes a unique variable for you to use? (I've not been in a position to need to know this stuff, so I am asking as I am learning

)

Posted: Tue Sep 27, 2005 12:47 am
by Ree
It's down!
