Page 1 of 1

Registry design pattern

Posted: Wed May 17, 2006 8:27 pm
by alex.barylski
I've read the article on phpPatterns and I'm not convinced of it's practicality...

In over a decade of professional development I have never (to my knowledge or my memory) had a problem caused by using globals...

Diligence and discipline have served me well I guess :P

Anyways, the more I read up on design patterns and familiarize myself with them I often find myself saying "ahhh..." I do that...or i've done that...and makes me feel good knowing now I have formalized my understanding of the subject...

And can at least follow along when I read posts by Arborint :P knowing the lingo makes a huge difference :)

Anyways...one pattern that I just have a helluva time accepting as reasonable is the Registry...

To make matters worse...when I search for Registry...all I get is PHP and Java articles...personally...I would prefer if it was done in C++...just cuz...

When I search other design patterns I have no problems...but registry I have failed to find anything outside of phpPatterns and a few Java articles...

Although I do get alot of articles on Windows registry :P

Anyways...before I ultimately conclude I don't need registry (or I find a bug caused by globals, etc) or it doesn't make sense...I'd like to read a quality article on the subject first...

Anyone know of any???

Cheers :)

Posted: Thu May 18, 2006 4:01 am
by Maugrim_The_Reaper
You're heading into questionable territory. While you are researching the Registry, also read up on the pros and cons of using the Singleton (related Pattern). Also, check out Registry alternatives (or complements?) like ServiceLocator and Dependency Injection. DI has been seeing a lot of attention - and the Sitepoint forums are full of material on the theory and implementation of DI in PHP, not much concensus though.

The basic no-no surrounding Globals, is that they are accessible everywhere even in places which don't need them. If a global variable has some importance attached and you don't want it messed with, then putting it out there where every piece of code in an app can access it represents a large risk. That's relative of course - but in general I don't trust Globals. For non-essential variables it works a treat, for anything else it's another risk I like to mitigate (usually by not using Globals if at all possible). Not evil - just potentially dangerous. You could just as easily ask why object properties need a public/private/protected declaration...

ServiceLocator is a nice option over a Registry. A ServiceLocator should actually instantiate objects for you (you can instantiate externally and then "register" the object to the ServiceLocator of course), where a Registry is more a storage container.