Magento系统中应用的12种设计模式

Magento its architecture is sometimes deemed overly engineered. If we look at it from a helicopter view, commonly used design patterns are easily spotted. Here are 12 of them.

Introduction

software design pattern is a reusable solution to an often occurring problem. This doesn’t mean that software is better if it
has more design patterns. Instead, a good software engineer should be able to spot the problem and implement the pattern instead of introducing implementations without purpose. The earlier behavior is leadingly noticeable in Magento, where most if not all
design pattern-implementations have a purpose.

This is a compilation of an article-series which originally appeared on Ryan
Street’s blog
 (@ryanstreet).

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.

Because Magento its views are “fat” – they often contain a lot of logic – its not rare that views have an additional PHP class (the Block system) which will help with rendering.

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.

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.

4. Singleton Pattern

Another way to retrieve an instance of a class, is to call Mage::getSingleton(). It accepts a class alias and before returning an instance, it checks the internal registry
whether this class has already been instantiated before – this results in a shared instance. An example of where this is mandatory, is the session storage which should be shared through the code base instead of creating it anew every time.

5. Registry Pattern

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.

6. Prototype Pattern

Where the factory pattern (#3 on our list) stops, is where the prototype pattern continues. It defines that instances of classes can retrieve a specific other class instance depending on its parent class (the prototype).
A notable example is the Mage_Catalog_Model_Product class which has a getTypeInstance method to retrieve the specificMage_Catalog_Model_Product_Type with
a specific subset of methods and properties not applicable to all products.

For example, the Mage_Downloadable_Model_Product_Type ultimately extends the Mage_Catalog_Model_Product_Type. If you are
iterating over an order and want to call a specific method of a downloadable product, you will need to factorize it first with the getTypeInstance method of the original product.

7. Object Pool Pattern

The object pool pattern is simply a box with objects so that they do not have to be allocated and destroyed over and over again. It’s not used a lot in Magento other than for heavy tasks where resources can get
limited soon, like importing products. The object pool (managed by Varien_Object_Cache) can be accessed with Mage::objects().

8. Iterator Pattern

The iterator pattern defines that there is a shared way to iterate over a container with objects. In Magento, this is handled by the Varien_Data_Collection which
on its turn uses various baked-in PHP classes (like ArrayIterator) for having a more OO-interface to arrays. This ensures that model-collections will always have a common API to iterate over without being dependent
of the actual models.

9. Lazy Loading Pattern

Lazy loading ensures that loading data is delayed until the point when it is actually needed. This results in less resources being used. One of the lazy loading behaviors of Magento is that of collections. If you
were to retrieve a collection of products with Mage::getModel('catalog/product')->getCollection(), the database will only be touched when you actually access the collection by, for example, iterating over it or
retrieving the count of models found.

10. Service Locator Pattern

The service locator pattern abstracts away the retrieval of a certain service. This allows for changing the service without breaking anything (as it adheres to its abstract foundation) but also fetching the service
as seen fit for its purpose.

Ryan exemplifies this with database connections. Another example is that
of Magento its caching mechanism where Mage::getCache() is a service locator by-proxy for the cache storage
supplied by Zend or other vendors.

11. Module Pattern

Anyone familiar with Magento development has stumbled upon the module pattern. It basically defines that different domains are grouped into separate modules which function independent of each other and can be plugged-in
to the main system as deemed appropriate. In an ideal situation, an implementation of the module pattern would make sure that each element can be removed or swapped. One of the protagonists of the module pattern in PHP is the Composer
package manager
.

Though Magento heavily relies on a modular architecture, its not modular to the bone. Certain functionality is heavily tied to the
core and can not be easily changed. There is also the heavy usage of the super-global Mage core-class which introduces all sorts of system-wide dependencies not easily overseen.

12. Observer Pattern

Magento its event-driven architecture is a result of an implementation of the observer pattern. By defining observers (or listeners), extra code can be hooked which will be called upon as the observed event fires.
Magento uses its XML-data storage to define observers. If an event is fired with Mage::dispatchEvent($eventName, $data), the data storage will be consulted and the appropriate observers for $event will
be fired.

In addition to using modules, events can be used to customize existing logic without touching the existing code.

Conclusion

Hopefully these 12 design patterns, give you a bit better understanding of the architectural decisions made in Magento. Not all design patterns found in Magento are implemented as defined by the original
literature
. This is perfectly fine, because design patterns are there to help with solving common problems as seen fit, instead of deploying verbatim implementations.

原文地址:http://magenticians.com/12-design-patterns-magento

时间: 2024-10-25 10:23:39

Magento系统中应用的12种设计模式的相关文章

Windows系统中常见的12种死机情况

在电脑使用过程中,我们经常会遇到死机的情况,而死机伴随者电脑操作系统,从一开始的Windows图形化操作系统开始,就一直有这样的情况,一直到Windows 7乃至泄漏版本的Windows 8一直存在. 为什么一直解决不了?那要从Windows的工作方式说起.为什么要强调Windows操作系统呢,因为除了Windows操作系统,还有另外的几大操作系统,最有名的有UNIX.UNIX的操作界面侧重于命令行界面,这样会减少许多资源.而Windows操作系统制作的初衷,是为了大众化,所以在图形化界面上下了

参透Node中exports的7种设计模式

前言 这篇文章试着要整理,翻译Export This: Interface Design Patterns for Node.js Modules这篇非常值得一读的文章. 但因为这篇文章有些时日了,部分示例已经不符合现况.故这是一篇加上小弟收集汇整而成的更新翻译. 旅程的开始 当你在Node中加载一个模块,我们到底会取回什么?当我们撰写一个模块时我们又有哪些选择可以用来设计程序的界面? 在我第一次学习Node的时候,发现在Node中有太多的方式处理这个问题,由于Javascript本身非常弹性,

碰撞系统中刚体的两种衰减力

一种是:Friction,即是摩擦力.一种是Damping,即是阻尼力. 摩擦力是,一种阻止两个相接触物体之间的相对位置移动的发生的力.阻尼力是,使振动能量随时间或距离逐步耗损的因素.如振动系统内部质点间相对运动的阻碍及外部介质摩擦等. 在碰撞系统中,摩擦力是用于计算碰撞体与碰撞体之间发生碰撞以后对力的衰减;阻尼力是直接对速度的衰减,即是不发生碰撞阻尼力也是存在的. 在Bullet中就是如此计算的.

最常用的12种设计模式小结_javascript技巧

1.策略模式(Strategy): 定义了算法家族, 分别封装起来, 让它们之间可以互相替换. 比如Collections.sort(List list, Comparator c); 可以通过实现多个Comparator接口来达到多种排序的目的. 2.装饰着模式(Decorator): 动态的给一个对象添加一些额外的职责. 比如java.io包. BufferedInputStream封装了FileInputStream, 它们都实现了InputStream接口, 但前者实现了readLine

Linux系统中的文件类型及文件扩展名详解

  Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux文件类型中衡量的话,大多是常规文件(也被称为普通文件). 一. 文件类型 Linux文件类型常见的有:普通文件.目录文件.字符设备文件和块设备文件.符号链接文件等,现在我们进行一个简要的说明. 1. 普通文件 我们用 ls -lh 来查看某个文件的属性,可以看到有类似-rwxrwxrwx,值得注意

Linux系统中的文件名和文件种类以及文件权限详解

  Linux文件种类与副文件名 一直强调一个概念,那就是:任何装置在Linux底下都是文件, 不仅如此,连资料沟通的介面也有专属的文件在负责-所以,你会瞭解到,Linux的文件种类真的很多- 除了前面提到的一般文件(-)与目录文件(d)之外,还有哪些种类的文件呢? * 文件种类: 我们在刚刚提到使用'ls -l'观察到第一栏那十个字元中,第一个字元为文件的类型. 除了常见的一般文件(-)与目录文件(d)之外,还有哪些种类的文件类型呢? * 正规文件(regular file ): 就是一般我们

PHP常用的三种设计模式汇总_php技巧

本篇文章是学习PHP中常用的三种设计模式的笔记及总结,不管采用哪一门语言开发什么,几乎都会使用到设计模式,我们为什么需要设计模式呢?它的诞生对于我们开发人员来说有什么样的作用与意义呢? 相信做iOS开发的人员对设计模式也会挺熟悉吧?比如单例设计模式.工厂设计模式.观察者模式.MVC框架结构设计模式等. 接下来我们一起来学习PHP中最常用的三种设计模式:单例设计模式.工厂设计模式和观察者设计模式. 单例设计模式 所谓单例模式,即在应用程序中最多只有该类的一个实例存在,一旦创建,就会一直存在于内存中

详解Linux系统中的文件名和文件种类以及文件权限

Linux文件种类与副文件名 一直强调一个概念,那就是:任何装置在Linux底下都是文件, 不仅如此,连资料沟通的介面也有专属的文件在负责-所以,你会?解到,Linux的文件种类真的很多- 除了前面提到的一般文件(-)与目录文件(d)之外,还有哪些种类的文件呢? * 文件种类: 我们在刚刚提到使用'ls -l'观察到第一栏那十个字元中,第一个字元为文件的类型. 除了常见的一般文件(-)与目录文件(d)之外,还有哪些种类的文件类型呢? * 正规文件(regular file ):       就是

谈谈23种设计模式在Android项目中的应用

前言 本文将结合实际谈谈23种设计模式,每种设计模式涉及 定义:抽象化的定义与通俗的描述,尽量说明清楚其含义与应用场景 示例:如果项目中有使用过该模式,则会给出项目中的代码,否则会给出尽可能简单好理解的java代码 Android:该设计模式在Android源码框架中哪些地方有使用到 重构:项目中是否存在可以用该模式进行重构的地方,如果有会给出重构前与重构后的代码或者思路 用这种方式进行介绍设计模式,旨在结合每天都在接触的Android实际项目开发更好地理解设计模式,拉近与设计模式的距离,同时在