views:

127

answers:

2

Magento is a newish (past 5 years) PHP based Ecommerce system with an architecture that's similar to the Java Spring framework (or so I've been told)

One of the features of the Framework is certain classes are not directly instantiated. Rather than do something like

$model = new Mage_Foo_Model_Name();

you pass an identifier into a static method on a global application object

$model = Mage::getModel('foo/name');

and this instantiates the class for you.

One of the wins with this approach is getModel checks a global configuration system for the foo/name identifier, and instantiates the class name it finds in the configuration system. This allows you to change the behavior of a Model system wide with a single configuration change.

Is there a formal, Gang of Four or otherwise, name that describes this system/design pattern? The instantiation itself looks like a classic Factory pattern, but I'm specifically interested in the whole "override a class in the system via configuration" aspect. Is there a name/concept that covers this, or is it contained within the worldview of a Factory?

+4  A: 

This looks like the Factory design pattern.


Quoting the wikipedia entry for Factory method pattern (emphasis mine) :

it deals with the problem of creating objects (products) without specifying the exact class of object that will be created.
The factory method design pattern handles this problem by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.

And :

Outside the scope of design patterns, the term factory method can also refer to a method of a factory whose main purpose is creation of objects.


A couple of other interesting links :

Pascal MARTIN
Yeah, the instantiating of the class looks like a factory, but I was more interested in the "allowing people to override things via. configuration" aspect of it. Question updated to better reflect what my mind wanted.
Alan Storm
+1  A: 

Sounds to me like a service locator.

This pattern uses a central registry known as the "service locator" which on request returns the information necessary to perform a certain task.

ColinD