Magento Design Patterns

2019-03-07 17:28发布

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

8条回答
做自己的国王
2楼-- · 2019-03-07 18:01

In general in my point of view Magento uses its own unique implementation of most patterns, so it should not be made that much of a comparing among them.

For example in the past I have seen the Factory creational pattern as a class, which handles the instantiation group classes. In Magento the merged config xml file stores the all paths to models, blocks and helpers, in order afterwards in the development process the developers to specify only the unique identifier for the path slash and the actual class name. The Singleton and the Registry patterns also is different than the expected.

查看更多
Luminary・发光体
3楼-- · 2019-03-07 18:02

I think that the relationship between Mage_Checkout_Model_Cart and Mage_Sales_Model_Quote is a Bridge design pattern. As defined by wikipedia Bridge is meant to "decouple an abstraction from its implementation so that the two can vary independently". Thus, Cart seems to be the abstraction and Quote seems to be the implementation.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-07 18:04

Don't forget about Lazy Loading, which means that database access only occurs when strictly necessary. For example:

$collection_of_products = Mage::getModel('catalog/product')
->getCollection();
$collection_of_products->addFieldToFilter('sku','n2610');

The database query will not be made until you attempt to access an item in the Collection.

查看更多
Luminary・发光体
5楼-- · 2019-03-07 18:09

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();
查看更多
贼婆χ
6楼-- · 2019-03-07 18:10

Following are design patterns : 1. Model View Control.

  1. Singleton

  2. Factory

  3. Registry

  4. Front controller.

  5. Iterator.

  6. Lazy Loading.

  7. Observers( events )

查看更多
干净又极端
7楼-- · 2019-03-07 18:11

Service Locator

Allows overrides or renamed physical resources (e.g. Classes, DB tables, etc)

Mage::getModel('catalog/product') and $installer->getTable('customer/address_entity')

查看更多
登录 后发表回答