<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Design Pattern on 凡·间</title>
    <link>https://16bh.github.io/tags/design-pattern/</link>
    <description>Recent content in Design Pattern on 凡·间</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Thu, 30 Mar 2017 17:18:15 +0800</lastBuildDate>
    <atom:link href="https://16bh.github.io/tags/design-pattern/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>[译]Magento中的12种设计模式</title>
      <link>https://16bh.github.io/2017/03/30/12-design-patterns-in-magento/</link>
      <pubDate>Thu, 30 Mar 2017 17:18:15 +0800</pubDate>
      <guid>https://16bh.github.io/2017/03/30/12-design-patterns-in-magento/</guid>
      <description>&lt;p&gt;发现一篇介绍magento的设计模式的英文文章，有空的时候翻译了一下&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;原文地址:
&lt;a href=&#34;http://magenticians.com/12-design-patterns-magento&#34;&gt;http://magenticians.com/12-design-patterns-magento&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;</description>
      <content:encoded><![CDATA[<p>发现一篇介绍magento的设计模式的英文文章，有空的时候翻译了一下</p>
<blockquote>
<p>原文地址:
<a href="http://magenticians.com/12-design-patterns-magento">http://magenticians.com/12-design-patterns-magento</a></p>
</blockquote>
<p>Magento中的架构通常被认为是过度设计了。如果从架构的角度去看Magento的代码，很容易发现它至少使用了下面介绍的的十二种设计模式。</p>
<blockquote>
<p>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.</p>
</blockquote>
<h2 id="介绍">介绍</h2>
<p>给出一个需求，解决的方案有很多，设计模式便是其中的最佳方法，可以解决特定环境下的同类问题。设计模式的代码是可重复利用的，对于提高工作效率大有裨益，那从长远角度考虑，是不是应当在代码中尽可能多的使用设计模式呢？明显不是，一名优秀的软件开发者知道何时使用设计模式，并不会无的放矢。早期Magento框架中的比较出色的一点是，其中的绝大部分（甚至所有）使用的设计模式的方式都有其用意所在（画外音：说明Magento的开发者是一名优秀的软件开发者，不然本文也就没有意义了）。</p>
<p>本文是根据@Ryan Street的系列博文整理而得的。</p>
<blockquote>
<p>Introduction</p>
</blockquote>
<p>A 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).</p>
<h2 id="模式1mvc">模式1：MVC</h2>
<p>简易程度：
使用场景：</p>
<p>Model-View-Controller,即模型-视图-控制器,简称MVC，应该是最广为人知的一种设计模式（大多数使用者甚至都不会将它视为设计模式）。</p>
<p>这是一种将业务逻辑、页面展示、逻辑分离开来的设计模式。Mageno中使用了大量的xml文件作为逻辑模板，使用pthml（混合了HTML和PHP）文件作为它的视图，剩下的模型依赖Varien的ORM。大多数的业务逻辑发生在模型中，而控制器将模型数据映射到视图，Magento的视图包含了太多的逻辑而显示很笨重，不得不通过一个专门的php类（Block类）进行渲染。</p>
<blockquote>
<p>Model View Controller Pattern</p>
</blockquote>
<p>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 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</p>
<h2 id="模式2-前端控制器模式">模式2： 前端控制器模式</h2>
<p>简易程度：
使用场景：</p>
<p>前端控制器模式确保有且只有一个入口。所有的请求都会先从前端控制器那里走一遭，被识别后路由分发到指定的controller，进行特定的处理。
在Magento唯一一个入口文件<code>index.php</code>就起到了前端控制器的作用，它通过<code>Mage::app()</code>方法实现应用环境的初始化并将请求路由到正确的controller中。</p>
<blockquote>
<p>Front Controller Pattern</p>
</blockquote>
<p>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.</p>
<h2 id="模式3工厂模式">模式3：工厂模式</h2>
<p>简易程度：
使用场景：
工厂模式的“工厂”二字已经充分表露了它的功能————如工厂的流水线一般统一进行类的实例化。</p>
<p>它被广泛应用在Magento的代码库中，负责自动加载系统。在<code>config.xml</code>文件中定义一个<code>module</code>的别名后，工厂就悄咪咪的记录了别名对应的类及类所在的位置。</p>
<p><code>Mage</code>核心类中有很多辅助实现工厂的方法，其中的<code>Mage::getModel()</code>方法可以接收一个类的别名返回类的实例，如<code>Mage::getModel('catalog/product')</code>返回产品类<code>Mage_Catalog_Model_Product</code>的实例。</p>
<p>区别于传统的在代码中直接引入类所在文件并调用类，工厂模式以统一的方式对类进行初始化。</p>
<blockquote>
<p>Factory Pattern</p>
</blockquote>
<p>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.</p>
<h2 id="模式4单例模式">模式4：单例模式</h2>
<p>简易程度：
使用场景：</p>
<p>另一种获取类的实例的方法是<code>Mage::getSingleton()</code>，它跟<code>Mage::getModel()</code>方法一样接收一个类的别名，不同之处在于<code>getSingleton </code>在返回实例之前，会先去注册表里瞄一眼，看看这个类是否已经实例化过了，如果实例化过了，那这个实例就可以被共享了。</p>
<p>例如，Magento中的session对象，（如customer session或checkout session）,被储存在注册表中，可以在代码不同地方重复使用，而不要每次都重新创建</p>
<blockquote>
<p>Singleton Pattern</p>
</blockquote>
<p>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.</p>
<h2 id="模式5注册模式">模式5：注册模式</h2>
<p>简易程度：
使用场景：
（进程级别的）</p>
<p>所有的单例都存储在内部注册表中,这是全局的存储数据的地方,而且不仅限于内部使用。下面列举的注册相关的方法可以分别实现从注册表中存储，查询，删除数据。</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-php" data-lang="php"><span class="line"><span class="cl"><span class="nx">Mage</span><span class="o">::</span><span class="na">register</span><span class="p">(</span><span class="nv">$key</span><span class="p">,</span><span class="nv">$value</span><span class="p">)</span>     <span class="c1">//存储
</span></span></span><span class="line"><span class="cl"><span class="nx">Mage</span><span class="o">::</span><span class="na">registry</span><span class="p">()</span>    <span class="c1">//查询
</span></span></span><span class="line"><span class="cl"><span class="nx">Mage</span><span class="o">::</span><span class="na">unregister</span><span class="p">()</span>  <span class="c1">//删除
</span></span></span></code></pre></div><p>这种注册表的方法通常应用于数据不能传递时的场景下，进行数据的传输。并且数据格式是<code>key-value</code>的格式</p>
<p>比如订单生成的时候<code>register</code>一个key，然后在<code>sales_order_save_after</code>事件的<code>observer</code>方法中通过<code>registry</code>读取之前<code>register</code>的key</p>
<blockquote>
<p>Registry Pattern</p>
</blockquote>
<p>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.</p>
<h2 id="模式6原型模式">模式6：原型模式</h2>
<p>简易程度：
使用场景：</p>
<p>原型模式是对工厂模式功能的补充,它定义类的实例可以根据其父类（原型）检索其它类的实例。</p>
<p>举个栗子，<code>Mage_Catalog_Model_Product</code>类有一个<code>getTypeInstance</code>方法来获取特定的类<code>Mage_Catalog_Model_Product_Type</code>的对象，后者包含了不适用于其它产品的一系列的方法和属性。
而<code>Mage_Downloadable_Model_Product_Type</code>这个Downloadable产品的类又最终继承了<code>Mage_Catalog_Model_Product_Type</code>类。如果您正在下单并想要调用Downloadable类型产品的特定方法，则需要首先使用原始的产品类的getTypeInstance方法对其进行实例化。</p>
<blockquote>
<p>Prototype Pattern</p>
</blockquote>
<p>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.</p>
<h2 id="模式7对象池模式">模式7：对象池模式</h2>
<p>简易程度：
使用场景：
对象池模式只是一个包含对象的集合，防止它们一次又一次被分配和销毁。</p>
<p>在Magento中，对象池模式并不常见，只会在处理严重影响服务器性能的重任务时被使用，例如批量导入产品的时候。</p>
<p>可以使用<code>Mage::objects（）</code>方法访问对象池（由<code>Varien_Object_Cache</code>类管理）</p>
<blockquote>
<p>Object Pool Pattern</p>
</blockquote>
<p>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().</p>
<h2 id="模式8-迭代器模式">模式8 迭代器模式</h2>
<p>简易程度：
使用场景：
迭代器模式定义了一个公共方法来遍历具有对象的结合。</p>
<p>在Magento中，这是由<code>Varien_Data_Collection</code>类实现的，它依次使用各种baked-in的PHP类（如<code>ArrayIterator</code>）来为数组提供更多的OO接口。这样可以确保模型集合始终具有一个通用的API来遍历，而不依赖于实际的模型。</p>
<blockquote>
<p>Iterator Pattern</p>
</blockquote>
<p>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.</p>
<h2 id="模式9延迟加载模式">模式9：延迟加载模式</h2>
<p>简易程度：
使用场景：</p>
<p>延迟加载确保加载数据被延迟到实际需要的时间点,这导致更少的资源利用。 Magento的延迟加载行为之一就是collection集合。如果使用<code>Mage::getModel（'catalog/product')-&gt;getCollection()</code>获取产品collection时，并没有操作数据库。只有当load之后，遍历collection中的product对象或者查询collection的数量时，才会对数据库进行读写操作。</p>
<blockquote>
<p>Lazy Loading Pattern</p>
</blockquote>
<p>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(&lsquo;catalog/product&rsquo;)-&gt;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.</p>
<h2 id="模式10服务定位器模式">模式10：服务定位器模式</h2>
<p>简易程度：
使用场景：</p>
<p>服务定位器模式抽取某个服务的检索。它遵守其抽象基础，可以在不破坏任何东西的情况下改变服务，而且可以看到适合其目的的服务。
例如，Ryan的数据库连接。</p>
<p>另一个例子是Magento的缓存机制，通过<code>Mage::getCache()</code>存储缓存，<code>Mage::getCache()</code>是由Zend或其他供应商提供的缓存存储的代理服务定位器。</p>
<p>再如，队列的实现</p>
<blockquote>
<p>Service Locator Pattern</p>
</blockquote>
<p>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.</p>
<h2 id="模式11模块模式">模式11：模块模式</h2>
<p>简易程度：
使用场景：</p>
<p>任何熟悉Magento开发的人都会很自然的接触模块模式。它基本上定义了不同的功能被分组成独立的模块，它们彼此独立，并且可以根据需要插入到Magento主系统中。在理想情况下，模块模式的实现将确保每个元素都可以被删除或交换。 PHP中模块模式的主角之一是Composer软件包管理器。
虽然Magento严重依赖于模块化架构，但它并不是模块化的。某些功能与核心密切相关，不能轻易改变。还大量使用超全局的<code>Mage</code>核心类，引入了各种不受监管的系统级依赖关系。</p>
<blockquote>
<p>Module Pattern</p>
</blockquote>
<p>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.</p>
<h2 id="模式12观察者模式">模式12：观察者模式</h2>
<p>简易程度：
使用场景：</p>
<p>Magento的事件驱动架构是实现观察者模式的结果。通过定义观察者（或监听者），可以挂接额外的代码，随着观察到的事件触发，这些代码将被调用。</p>
<p>Magento使用<code>config.xml</code>存储并定义观察者。使用<code>Mage::dispatchEvent（$eventName，$data）</code>触发事件，eventName是事件名，data是参数。事件触发后会将查询数据存储，并触发相应的<code>$event</code>观察者。
除了使用模块之外，还可以使用事件来定制现有的逻辑，而不用接触现有的代码。</p>
<p>举个栗子，下单完成后发送邮件通知用户，不需要将写邮件的代码跟下单的代码写在一起，只要下单完成后触发事件<code>sales_order_save_after</code>,在<code>config.xml</code>中定义<code>sales_order_save_after</code>事件的观察者，然后在观察者方法中写相应的发送邮件的代码</p>
<blockquote>
<p>Observer Pattern</p>
</blockquote>
<p>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.</p>
<h2 id="补充这篇翻译里的两个实际判断">补充：这篇翻译里的两个实际判断</h2>
<p>这是一篇关于 Magento 1 架构的设计模式综述，内容本身准确度不错（原文出处是 Ryan Street 的系列）。补两点读的时候需要知道的事。</p>
<h3 id="magento-是过度设计这个判断的语境">“Magento 是过度设计”这个判断的语境</h3>
<p>原文开篇说“Magento 的架构有时被认为是过度设计了”。这句话在 2017 年很流行，但要说清它的<strong>语境</strong>：Magento 1 是为“多店铺、多语言、可被第三方模块深度定制”设计的电商平台，它的 XML 配置驱动、完全可覆盖的模型层、事件系统，都是为这个目标服务的。</p>
<p>对一个只卖 10 个 SKU 的简单商城来说，这些确实过重；但对需要接 ERP、接支付、接第三方物流的复杂业务，它换来的好处是<strong>不用改核心代码就能扩展</strong>。所以“过度设计”的评价取决于需求规模，不是框架本身的问题。</p>
<p>这个判断后来被市场验证了一部分：Magento 2 在保持同样架构理念的同时大幅简化了目录结构，但依然被认为学习曲线陡峭——说明复杂性有一部分来自电商业务本身。</p>
<h3 id="这-12-种模式在今天还值得看">这 12 种模式在今天还值得看</h3>
<p>虽然讲的是 Magento 1，但这 12 个模式中的多数是<strong>框架通用</strong>的，换个语言照样适用：</p>
<table>
	<thead>
			<tr>
					<th>模式</th>
					<th>Magento 里的载体</th>
					<th>今天的对应</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>观察者</td>
					<td><code>config.xml</code> 里的事件监听</td>
					<td>任何事件总线、Spring 的 <code>@EventListener</code></td>
			</tr>
			<tr>
					<td>单例</td>
					<td><code>Mage::getSingleton</code></td>
					<td>DI 容器里的单例作用域</td>
			</tr>
			<tr>
					<td>工厂</td>
					<td><code>Mage::getModel</code></td>
					<td>工厂方法、容器 <code>make()</code></td>
			</tr>
			<tr>
					<td>对象池/原型</td>
					<td><code>Mage::getModel</code> 每次返回新实例</td>
					<td>原型作用域</td>
			</tr>
	</tbody>
</table>
<p>真正值得留意的是**“Magento 把设计模式做成了框架能力，而不是要求开发者手写”**这条：你写业务代码时几乎不会主动 new 一个工厂，因为 <code>Mage::getModel</code> 本身就是工厂。这种“模式内建在框架里”的做法，今天的 Laravel、Spring 也一样。</p>
<h3 id="翻译里的一处笔误">翻译里的一处笔误</h3>
<p>文中写“使用 pthml（混合了 HTML 和 PHP）文件作为它的视图”，正确拼写是 <strong>phtml</strong>（PHP + HTML）。这是 Magento/Laminas 体系里对 <code>.phtml</code> 视图模板的叫法，不影响阅读，记一下免得写错。</p>
<h3 id="关于模式越多越好的回答">关于“模式越多越好”的回答</h3>
<p>原文有一段讲“不能为了用模式而用模式”，这是全文最有价值的部分。补一个具体的判断标准：<strong>当你无法用一句话说出“这个模式解决了什么具体问题”时，就不该引入它</strong>。设计模式的收益是“未来修改的便利”，成本是“当下阅读的负担”；没有预见到具体变化点的抽象，都是在还看不见的债。</p>
<p><em>本文整理于 2017-03，是英文文章的翻译稿。上述补充不改变译文内容，只补充语境说明。</em></p>]]></content:encoded>
    </item>
  </channel>
</rss>
