10
votes

There are a few very obvious patterns in use in Magento's core, such as:

  • Singleton
  • Registry
  • Event/Observer
  • Factory
  • Model/View/Controller

But there are others that may be in use in Magento that I'm not aware of such as Actor, Decorator, Strategy patterns.

Is there a reference list of the usage of all pattern types in Magento or how to implement in third party modules that don't unnecessarily replicate built-in functionality of Magento?

locked by philwinkle Dec 9 '15 at 4:26

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

Read more about locked posts here.

12
votes

There is a list of design patterns which are used in Magento on Stack Overflow

From the article:

The obvious ones are:

Factory:

$product = Mage::getModel('catalog/product');

Singleton:

$category = Mage::getSingleton('catalog/session');

Registry:

$currentCategory = Mage::registry('current_category');

View Helper:

Mage::helper('core');

Prototype:

Mage:getModel('catalog/product')->getTypeInstance();

Object Pool:

$id = Mage::objects()->save($object);
$object = Mage::objects($id);

Iterator:

Mage::getModel('catalog/product')->getCollection();

Event/Listeners:

Mage::dispatchEvent('model_load_before', $params);

EDIT

Active record

$product->save()
$product->getName()

Null object

$collection->getFirstItem()

Lazy loading - collections are loaded when iterated

There are also 5 articles on Ryan Street blog about MVC, Front Controller, Factory, Singleton and Registry in Magento

Edit
I might add that the Magento 'singleton' is more a pseudo-singleton. It's a combination of 'Factory' because Mage::getSingleton() is a factory and 'Registry' because inside the getSingleton() method this is called

self::register($registryKey, self::getModel($modelClass, $arguments))
  • 2
    I might add 'Active record' ($product->save(), $product->getName(), ...), 'Null object' ($collection->getFirstItem()), 'Front controller', `Lazy loading' (collections are loaded when iterated) – Marius May 14 '13 at 21:18
  • @Marius feel free to edit the answer. – philwinkle May 14 '13 at 21:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.