《Contributing to Eclipse》提出的「分層法則」核心精神:語言無關功能與特定語言功能分離、核心功能與 UI 功能分離。本文結合實務經驗,說明如何在架構設計階段落實此法則。
典型產出:底層基礎模組(資源管理、執行時模型、Workbench 等)與上層功能模組(如基於 GEF 的編輯器)。底層提供服務,上層消費服務。
每個功能模組(含底層)一律拆分為:
com.myproduct.xxx.core:領域模型、業務邏輯、非 UI 服務。com.myproduct.xxx.ui:視圖、編輯器、Wizard、Action 等 UI 程式碼。範例結構:
com.myproduct.myeditor.core // EMF 模型 com.myproduct.myeditor.ui // GEF 編輯器 UI com.myproduct.resource.core // Builder 邏輯 com.myproduct.refactoring.core // 重構核心演算法 com.myproduct.refactoring.ui // 重構 Wizard、對話框
錯誤:上層 Core 直接依賴底層 UI(如 myeditor.core 呼叫 refactoring.ui 類別)。
正確:上層 Core 僅依賴底層 Core;UI 層之間可依賴(myeditor.ui → refactoring.ui),或上層 UI 依賴底層 Core。
// 錯誤依賴 myeditor.core → refactoring.ui (✗) // 正確依賴 myeditor.core → refactoring.core (✓) myeditor.ui → refactoring.ui (✓) myeditor.ui → refactoring.core (✓)
org.eclipse.ui、org.eclipse.jface)。*.core、*.ui 兩個 Plug-in,以物理結構強制約束。遵循分層法則不僅符合 Eclipse 平台慣例,更能提升模組可測試性、可複用性與團隊並行開發效率。