3

I want to know how many 'Design Patterns' are magento using, I am exploring my self and found below lists, if you know others than listed patterns. Please feel free to add.

1: MVC - Magento utilizes a unique MVC pattern, utilizing a DOM based configuration layer. It leverages xml to drive the configuration and actions of the application on top of the regular Model-View-Controller architecture.

2: Front Controller - Magento uses the Front Controller pattern to implement workflows for it's application. It has a single entry point (index.php) for all of it's requests. Let's see how it all works.

3: Factory - The Factory Method is used to instantiate classes in Magento. You instantiate a class in Magento by calling an appropriate method passing an abstract name representing a class group followed by a class name. Class groups and their appropriate abstractions are declared in your configuration XML files in your module's /etc/ folder.

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');

4: Singleton - Much like factory class abstraction and class groups in Magento, the Singleton pattern is instantiated for Blocks and Classes just the same. It restricts the instantiation of a class to one object. It will refer to same object each time called. $category = Mage::getSingleton('catalog/session');

5: Registry - he registry pattern is basically a pattern that allows any object or data to be available in a public global scope for any resource to use. It is a way to store information throughout your application. Mage::register('key',$value); //stores $currentCategory = Mage::registry('key'); //retrives

6: Prototype - The Prototype pattern in Magento is used as an extension of the Abstract Factory pattern. It ensures that an appropriate subclass is instantiated via appropriate types that are assigned to an object. What does this mean? Basically, it means that whenever you need to get a specific class that is defined via its parent type, the prototype pattern ensures you get the right class that can handle what you need. 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();

7: Object Pool - The Object Pool Pattern keeps objects ready for use over and over again instead of re-instantiating them and destroying them once finished. It is a great way to save on memory consumption and compute cycles. It is used to reuse and share objects that are expensive to create. $id = Mage::objects()->save($object); $object = Mage::objects($id);

8: Iterator - The Iterator Pattern is a design pattern that allows an object traverse through the elements of another class. This allows you to specify an iterator and allow for multiple different sets of data to be passed without changing the underlying structure that allows the iteration. It is used to traverse a collection and access the collection’s items.

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

9: Lazy Loading - Lazy Loading is a design pattern that delays the loading of an object until the time that the object is called upon. With Magento, they don't utilize this with objects, but data. 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();

10: Service Locator - The service locator is a design pattern that allows a user to get a service by encapsulating the process inside an abstraction layer. This allows the user to retrieve the appropriate or best service without knowing what that service is at runtime.

Allows overrides or renamed physical resources (e.g. Classes, DB tables, etc) Mage::getModel('catalog/product') and $installer->getTable('customer/address_entity');

11: Module - The Module Design Pattern is a form of modular programming that emphasizes the grouping of functionality of a program into independent, interchangeable modules.

12: Event/Observer - The observer pattern is where an event listener is set at a certain point during an application's execution. Other components of the application can "hook" into this event listener and execute their code during this point. 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));

**

*>

<global>
    <events>
        <event_name>
            <observers>
                <unique_name>
                    <class>Class_Name</class>
                    <method>methodName</method>
                </unique_name>
            </observers>
        </event_name>
    </events>
</global> </config>*

**

13: View Helper / Helper : It will react as a to access the helper classes. 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');

14: Null object - Provide an object as a surrogate for the lack of an object of a given type. / The Null Object Pattern provides intelligent do nothing behavior, hiding the details from its collaborators. $collection->getFirstItem();

15 : Decorator: It is used to extend or modify the behaviour of an object at runtime. Add additional responsibilities dynamically to an object.

decorateTable('product_comparison');
  • decorateTable('product_comparison'); is not the decorator design pattern. It's just a simple js function that adds classes to tr and td elements. the name is misleading. – Marius Jan 8 '15 at 10:10
  • You REALLY should credit your sources especially when you copy their content directly - and when they are a Magento U instructor: coolryan.com/magento-design-patterns – benmarks Jan 8 '15 at 11:07
  • Okay Benmarks, will include.. – Abhishek Gupta Jan 8 '15 at 11:10
  • what about magento2? – sivakumar Apr 4 '17 at 18:12