Feb 25, 2013
kalpesh

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

  • as we need

  • Thank you 🙂

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions