Magento: Design Patterns
Factory:
It implement the concept of factories and deals with the problem of creating objects without specifying the exact class of object that will be created.
$product = Mage::getModel('catalog/product'); |
Singleton:
It restricts the instantiation of a class to one object. It will refer to same object each time called.
$category = Mage::getSingleton('catalog/session'); |
Registry:
It is a way to store information throughout your application.
Mage::register('key',$value); //stores | |
$currentCategory = Mage::registry('key'); //retrives |
Prototype:
It determines the type of object to create. In Magento it can be Simple, Configurable, Grouped, Bundle, Downloadable or Virtual types.
Mage:getModel('catalog/product')->getTypeInstance(); |
Observer:
It is mainly used to implement distributed event handling systems. Here the subject maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
Mage::dispatchEvent('event_name', array('key'=>$value)); |
<config> | |
<global> | |
<events> | |
<event_name> | |
<observers> | |
<unique_name> | |
<class>Class_Name</class> | |
<method>methodName</method> | |
</unique_name> | |
</observers> | |
</event_name> | |
</events> | |
</global> | |
</config> |
Object Pool:
It is used to reuse and share objects that are expensive to create.
$id = Mage::objects()->save($object); | |
$object = Mage::objects($id); |
Iterator:
It is used to traverse a collection and access the collection’s items.
Mage::getModel('catalog/product')->getCollection(); |
Lazy Loading:
It is used to defer initialization of an object until the point at which it is needed.
$collection_of_products = Mage::getModel('catalog/product') | |
->getCollection(); |
Decorator:
It is used to extend or modify the behaviour of an object at runtime.
<script type="text/javascript">decorateTable('product_comparison');</script> |
Helper:
Multiple methods are available for use by other objects. Here you can use core’s helper methods from anywhere in the application.
Mage::helper('core'); |
Service Locator:
Allows overrides or renamed physical resources (e.g. Classes, DB tables, etc)
Mage::getModel('catalog/product') and $installer->getTable('customer/address_entity'); |
Thanks to contributors at: http://stackoverflow.com/questions/5041473/magento-design-patterns
2 Comments
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
as we need
Thank you 🙂