Tag Archives: Design Pattern

Design Pattern

Difference between Mage::getSingleton() and Mage::getModel() in Magento

Hello all of you today I will explain you what is Difference between Mage::getSingleton() and Mage::getModel() in Magento.

Mage::getSingleton()

Mage::getSingleton() will first check the same class instance is exits or not in memory. If the instance is created then it will return the same object from memory. So Mage::getSingleton() faster then Mage::getModel().

$product1 = Mage::getSingleton('catalog/product');
$product2 = Mage::getSingleton('catalog/product');

$product1 and $product2  both will share same memory of OS and return only one instance each time. 

Mage::getgetModel()

Mage::getModel() will create a new instance of an object each time even such object exists in configuration.

Example

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

$product1 and $product2 both have different instant of same object and also occupy different memory .

Hope you like this. 🙂 🙂

Magento: Design Patterns

Magento Design Pattern
Magento Design Pattern

The list of design pattern Which Magento used.

  • Model/View/Controller

Magento follow Model/View/Controller design pattern.

  • Singleton

Ensure that only one instance of a class is created and Provide a global access point to the object.

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

Creates objects without exposing the instantiation logic to the clientand Refers to the newly created object through a common interface.

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

It will provide you one way to store your information throughout your application.

$currentCategory = Mage::registry('current_category');
  • Prototype

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

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

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Mage::dispatchEvent('model_load_after', $params);
  • Object Pool

Reuses and shares objects that are expensive to create.

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

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

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

It will react as a to access the helper classes.

Mage::helper('core');
  • 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();
  • Decorator

Aadd additional responsibilities dynamically to an object.

<script type="text/javascript">
     decorateTable('product_comparison');
</script>

Hope you  like article. Thanks for reading.