你想聚焦在 Magento 2 CE(Community Edition / Open Source)與 EE(Enterprise / Commerce) 的 設計模式(Design Patterns)。
這部分是 Magento 2 架構的核心精髓 —— 不論 CE 還是 EE(後者只是多了功能模組與企業支援),它們共用相同核心框架與設計模式。
Magento 2 採用了大量 企業級設計模式(Enterprise Design Patterns),以達到「高度可擴充、低耦合、可配置化」的目的。
以下按層次與功能分類說明 👇
透過 XML (di.xml) 或 Constructor Injection,Magento 2 讓所有依賴都由 DI Container 自動生成與注入。
這是 Magento 2 的最核心設計模式,取代 Magento 1 的「Object Manager 濫用」。
class OrderService
{
protected $orderRepository;
public function __construct(
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository
) {
$this->orderRepository = $orderRepository;
}
}
在 di.xml 中:
<preference for="Magento\Sales\Api\OrderRepositoryInterface" type="Magento\Sales\Model\OrderRepository"/>
📍EE / CE 一樣使用:EE 中也透過 DI 取代特定服務(例如 GiftCard 模組、TargetRule 模組)。
Magento 2 的 Factory 類是由 setup:di:compile 自動生成,供開發者動態建立物件。
class Example
{
protected $orderFactory;
public function __construct(
\Magento\Sales\Model\OrderFactory $orderFactory
) {
$this->orderFactory = $orderFactory;
}
public function createOrder()
{
return $this->orderFactory->create();
}
}
📍EE / CE 差異:EE 模組也大量用 Factory 生成 entity,例如
\Magento\Reward\Model\RewardFactory。
事件機制允許在系統發生特定動作時觸發外部行為,不需修改核心程式。
events.xml:
<event name="sales_order_place_after">
<observer name="custom_order_observer" instance="Vendor\Module\Observer\OrderObserver" />
</event>
OrderObserver.php:
class OrderObserver implements ObserverInterface
{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$order = $observer->getEvent()->getOrder();
}
}
📍EE 中常用:Enterprise 模組(如 GiftCard、Reward、TargetRule)常在 checkout 流程中注入 observer。
Magento 2 的 AOP(面向切面)實現,可攔截任何 public 方法前/後/包裹呼叫。
di.xml:
<type name="Magento\Checkout\Model\Session">
<plugin name="custom_session_plugin" type="Vendor\Module\Plugin\SessionPlugin" />
</type>
SessionPlugin.php:
class SessionPlugin
{
public function beforeSetQuote($subject, $quote)
{
// 在 setQuote 前執行
}
public function afterGetQuote($subject, $result)
{
// 修改回傳值
return $result;
}
}
📍EE / CE 同樣使用:EE 模組的銷售規則 / 獎勵金模組,皆以 plugin 攔截核心方法。
定義資料操作的「合約層」(interface),實作於 Model/Repository.php。
在 Service 層以 interface 暴露功能給外部模組(甚至 API)。
Api/ProductRepositoryInterface.php:
interface ProductRepositoryInterface
{
public function getById($id);
}
Model/ProductRepository.php:
class ProductRepository implements ProductRepositoryInterface
{
public function getById($id)
{
return $this->productFactory->create()->load($id);
}
}
📍EE 尤其重要:EE 提供大量 API (SOAP/REST),都是基於 Service Contracts。
允許在執行時替換不同演算法(例如稅率計算、折扣規則、配送策略)。
不同的 tax calculation class 可透過 DI 選擇策略。
<type name="Magento\Tax\Model\Calculation">
<arguments>
<argument name="calculator" xsi:type="object">Vendor\Module\Model\Tax\CustomCalculator</argument>
</arguments>
</type>
📍EE 模組常見:B2B 價格規則、動態優惠券、Advanced Pricing 全都透過策略注入。
部分核心物件由 Object Manager 管理為單例,例如 Magento\Framework\App\Config、Magento\Framework\Event\Manager。
📍開發者不應手動建立單例,而是透過 DI。
例如:
整個 Magento 以 Front Controller (index.php) 處理所有請求,並透過 router 決定進入哪個 module 的 controller/action。
Magento 2 在 MVC 上又多加一層 Service Layer 與 ViewModel:
📍EE / CE 架構相同,Commerce 版只是多出更多 service contracts。
Request
↓
Front Controller
↓
Router → Controller → Action
↓
Service Layer (Repository + Service Contract)
↓
Model / ResourceModel / Collection
↓
ViewModel → Block → Layout → Template
雖然核心模式一致,但 EE 中有更多企業模組展示了高階模式的實戰應用:
| 模組 | 使用的設計模式 | 說明 |
|---|---|---|
| Advanced Catalog Permissions | Strategy + Observer | 根據使用者群組控制目錄存取 |
| Reward Points | Plugin + Observer + Decorator | 攔截訂單事件、增加回饋邏輯 |
| GiftCard / StoreCredit | Factory + Repository + Service Contract | 高可擴充、API 友善 |
| Staging / Preview | Command + Strategy | 不同版本內容策略切換 |
| Page Builder | Composite + Builder Pattern | 建立可視化內容結構 |
| 類別 | 設計模式 | 功能目的 |
|---|---|---|
| 核心依賴 | DI / Factory / Proxy | 管理物件生命週期與依賴 |
| 邏輯擴充 | Observer / Plugin | 無侵入擴充核心邏輯 |
| 資料層 | Repository / Service Contract | API 穩定性、可替換性 |
| 行為策略 | Strategy / Command / Decorator | 動態切換邏輯或包裝行為 |
| 架構層次 | MVC / ViewModel / Front Controller | 分離視圖與商業邏輯 |
| 系統級模式 | Composite / Singleton | 結構化與效率 |
如果你想更進階,我可以幫你整理:
要我幫你做哪一個?