Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Design Patterns

Magento, IMHO, represents a PHP system that is built on well thought-out coding principles - reuseable design patterns being one of them. In terms of an example of a PHP system, I think it can be considered pretty cutting edge and therefore worth considering from an architectural point of view.

As I understand it, there are many design patterns that are available to the OOP developer. Seeing such patterns being put to use in an open-source system such as Magento allows a developer to view examples of such patterns in real use and in situ, rather than in examples that can sometimes be rather achedemic, and even a little misleading.

As such, I am wondering what patterns, other than the ones I have listed below, Magento programmers have used when developing for Magento.

As a note, I understand that some of these patterns are in place as a consequence of being built on the Zend Framework, MVC / Front Controller being a couple of them,

The obvious ones are:

Factory:

$product = Mage::getModel('catalog/product'); 

Singleton:

$category = Mage::getSingleton('catalog/session'); 

Registry:

$currentCategory = Mage::registry('current_category'); 
like image 637
JonB Avatar asked Feb 18 '11 12:02

JonB


People also ask

What is Singleton design pattern in Magento 2?

One of the architectural goodies of Magento is it's Singleton design pattern. In short, Singleton design pattern ensures a class has only one instance. Therefore one should provide a global point of access to that single instance of a class.

What is Magento 2 registry pattern?

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. Mage::unregister('identifier'); This is especially helpful transferring data between Models and Blocks without having to instantiate an entire class and load data.

What is the architecture of Magento 2?

Magento 2 has a totally different architecture than Magento 1. Its architecture is designed with the objective of making the source code as an extensive and modularized as possible. The main purpose of this approach is to allow it to be easily adapted and customized according to the need of the project.


1 Answers

Prototype:

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

Event-Observer Pair:

# PHP Mage::dispatchEvent('event_name', array('key'=>$value));  # config.xml <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:

$id = Mage::objects()->save($object); $object = Mage::objects($id); 

Iterator:

Mage::getModel('catalog/product')->getCollection(); 
like image 186
clockworkgeek Avatar answered Oct 02 '22 01:10

clockworkgeek