What is a design pattern exactly?

design-pattern

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.

A design pattern is a reusable solution to a common problem. That is as perfect a description.

The list of design pattern Which Magento used:

1. Model View Controller Pattern

Model View Controller, MVC for short, is a design pattern where business, presentation and coupling logic are separated. Magento heavily utilizes XML as templating-logic and HTML mixed with PHP files for its views. Models are backed by Varien’s ORM. Most business logic happens in the models whereas the controllers map the model-data to the views.

2. Front Controller Pattern

The front controller pattern makes sure that there is one and only one point of entry. All requests are investigated, routed to the designated controller and then processed accordingly to the specification. The front controller is responsible of initializing the environment and routing requests to designated controllers.

Magento has only one point of entry (index.php) which will initialize the application environment (Mage::app()) and route the request to the correct 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.

3. Factory Pattern

As implied by the name, the factory pattern is responsible of factorizing (instantiating) classes. It’s widely used through the Magento code base and leverages the autoloading system in Magento. By defining an alias in a module its config.xml you are letting the factory know where it can find classes.

There are various factory-helper methods in the Mage core class and one of them is getModel(). It accepts an alias for a class and will then return an instance of it. Instead of having include calls scattered through the code base, the factory pattern will instantiate classes in an uniform way.

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.

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

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.
you can now call a new model from your Model folder by the method, Mage::getModel().

Example: Mage::getModel(‘abstractName/setup’); 
This will call Namespace_Modulename_Model_Setup. Also, due to the autoload() method used in Magento, it will look for a file located in app//Namespace/Modulename/Model/Setup.php.

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

5. Registry

All the singletons are stored in the internal registry: a global scoped container for storing data. It is not only for internal use. The Mage::register($key, $value), ::registry($key) and ::unregister($key) methods can be respectively used for storing, retrieving and removing data from the registry. The registry is often used for transferring data between scopes when they cannot be passed on, otherwise.

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


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

6. 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);

7. 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();

8. Object Pool

Reuses and shares objects that are expensive to create.


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

9. Iterator

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

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

10. View Helper

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

You can call a new helper class by using Mage::helper(‘abstractName’); If no file declaration is used, it is assumed that ‘Data’ is what is being called.

Example: Mage::helper(‘abstractName’) calls Namespace_Modulename_Helper_Data located in app//Namespace/Modulename/Helper/Data.php

11.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

Categories: Magento

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *