“Gang of Four” Design Patterns in Magento Core
-
16-10-2019 - |
Pergunta
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?
Solução
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))