Design patterns in Magento

Care to share?

Design patterns have been used in software engineering for a long time and using them in contemporary programming is not only a standard, but also a must. Thanks to them, code is more organized and easier to develop, which is extremely important in the case of Open Source applications.

Although the use of patterns is mandatory, their implementation in a given system depends only on its authors. More complex applications often make use of not one, but several patterns. Magento can utilize even a dozen, at least so many I was able to discover. Below I present a hopefully complete list of design patterns used by the platform with brief examples of their use in the code.

Model View Controller

Currently, it’s probably the most popular model used in programming. It divides an application into three parts:

  1. Model – containing business logic
  2. View – responsible for the way in which an application is presented to a user
  3. Controller – receives data from a user, transfers it to the model, and manages views

The use of the pattern by Magento

The view section took over some of the responsibilities of the controller section and has been divided into layout – an xml file that defines what and where to display on a page, template – a phtml file responsible for the manner of displaying a particular item, and block – responsible for retrieving data from the model and passing it to the template.

Module  1

Factory method

Interface responsible for instantiating classes.

The use of the pattern by Magento

The implementation of this pattern can be found in Mage base class in such methods like, e.g. getModel(‘class_group/class) or helper(‘class_group/class), to which as a parameter we transfer an abstract name of a class which instance we want to create.

Cod 1


Specifies that at any given time there can be only one instance of a given class, and allows global access to it.

The use of the pattern by Magento

You just need to request the method Mage::getSingleton(‘class_group/class’). Interestingly, using the method Mage::getModel(‘class_group/class’), we still have the possibility to create new instances of a particular object. The Registry was used to implement this method.


Allows you to place objects and data in the global pool, allowing access to them from anywhere in the code.

The use of the pattern by Magento

Adding something to the registry in Magento is done by requesting the method

Mage::register($uniqueKey, $value), reading with the use of

Mage::registry($uniqueKey). To delete a value from the registry we can use the method Mage::unregister($uniqueKey).

3 Cod


It involves defining the Observers responsive to specific events in the code.

The use of the pattern by Magento

It is because of this pattern Magento architecture is defined as event-driven. Implementing the pattern in Magento allows you to change the application logic without having to modify its code. Observers definitions can be found in the configuration files of modules, while the Event itself is requested with the method Mage::dispatchEvent ($ EventName, $ dataPassedToTheObserver).

4 Cod
5 Cod
6 Cod

Front Controller

This pattern defines the existence of a single point of entry to the application.

The use of the pattern by Magento

In Magento all requests go through index.php, which is responsible for the initialization of the entire application.


Allows objects to create other instances of classes specific to themselves.

The use of the pattern by Magento

One example is Mage_Catalog_Model_Product class with a method getTypeInstance(), which returns a class instance specific for a given product type.

8 Cod

Lazy Loading

This pattern informs us that performing costly operations is delayed until they are needed.

The use of the pattern by Magento

Magento uses this pattern with collections that retrieve information from the database only when we try to refer to them.


Provides sequential access to objects grouped under another larger object.

The use of the pattern by Magento

This is another pattern used by collections in Magento. It allows us to use them as normal tables.

10 Cod

Service locator

Encapsulates a service within the extended abstract layer. This allows us to change the service we use without affecting the entire application.

The use of the pattern by Magento

Examples of using this pattern in Magento are methods: Mage::getCache() – responsible for cache, and _getConnection() from Mage_Core_Model_Resource_Db_Abstract class returning database ORM.

Object pool

Instead of destroying the objects, the system puts them into the pool from which they can be retrieved and used again.

The use of the pattern by Magento

In Magento this pattern can be found in Varien_Object_Cache class, accessible through the method Mage::objects()


Organizes the code in specialized and independent sets, grouping particular functionalities. With this approach it is possible to remove or replace a functionality without negatively affecting the rest of the system.

The use of the pattern by Magento

The use of this pattern in Magento and the way it was designed is undoubtedly one of its greatest advantages. Magento is entirely composed of modules – see for yourselves in the directory app/code/core/Mage. Each folder you will find there is a separate module. Maybe they are not as independent as the definition of a pattern would require, but the whole architecture allows for a relatively free extending and modification of the whole system through custom modules.

Core 1

Empty Object

Uses objects that are neutral in behavior. Such objects are designed to replace others that were not found. They should offer the same set of methods as the searched object but they should not perform any operations.

The use of the pattern by Magento

Methods getFirstItem() and getLastItem() from Varien_Data_Collection class are examples of the pattern in Magento.  When the collection is empty, they return an instance of the base class which the collection is referenced to.

12 Cod

Active record

Objects are a representation of a row in the database table. These objects should have properties that reflect the columns representing the structure of the table, and methods to allow modifications of these properties in the database.

The use of the pattern by Magento

The classes that inherit after Mage_Core_Model_Abstract class have access to load(), save() and delete() methods that allow loading, modification, creating or deleting records in a table that the class is connected with. Additionally, Mage_Core_Model_Abstract class inherits from Varien_Object, which gives us access to truly magical methods __set() and __get() that are responsible for automatic mapping of columns in a database table with the properties of a given object.

13 Cod


Do you want to improve your e-Business with Divante? Contact us.

Published May 4, 2015