Magento Singleton Design Patterns

The Singleton pattern in magento is instantiated for Blocks and Classes same as factory class abstraction and class groups in Magento .
Instead of the regular getModel() method, you now call:

<?php Mage::getSingleton('abstract/value'); ?>

If model was already instantiated singleton will return the instance, otherwise it will create a new one. Quite handy because in some cases you don’t want to create another instance to be created (e.g. config, order, quote etc).

 

Like as below:

<php 

   /**
     * Retrieve model object singleton
     *
     * @param   string $modelClass
     * @param   array $arguments
     * @return  Mage_Core_Model_Abstract
     */
    public static function getSingleton($modelClass='', array $arguments=array())
    {
        $registryKey = '_singleton/'.$modelClass;
        if (!self::registry($registryKey)) {
            self::register($registryKey, self::getModel($modelClass, $arguments));
        }
        return self::registry($registryKey);
    }

?>

 

For More About Design Patterns Click here

Leave a Reply

Your email address will not be published.