close
Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": /usr/lib/python2.7/dist-packages/libsvn/_delta.so: failed to map segment from shared object: Cannot allocate memory). Look in the Trac log for more information.
- Timestamp:
-
Aug 24, 2011, 5:18:31 PM (14 years ago)
- Author:
-
waue
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
v3
|
v4
|
|
25 | 25 | * 使用核心所需要的資源也是很小的 |
26 | 26 | * 非侵入性(Nonintrusive)框架,它的目的之一,是讓應用程式不感受到框架的存在,減低應用程式從框架移植時的負擔。 |
27 | | |
28 | 27 | * 容器(Container) |
29 | 28 | * 管理物件的生命週期、物件的組態、相依注入等 |
… |
… |
|
48 | 47 | = IoC 模式 = |
49 | 48 | |
50 | | |
51 | | IoC模式基本上是一個高層的概念,在 Martin Fowler 的 Inversion of Control Containers and the Dependency Injection pattern 中談到,實現IoC有兩種方式:Dependency Injection與Service Locator,Spring 所採用的是Dependency Injection 來實現 IoC,中文翻譯為依賴注入,依賴注入的意義是:「保留抽象介面,讓組件依賴於抽象介面,當組件要與其它實際的物件發生依賴關係時,藉過抽象介面來注入依賴的實際物件。」 |
| 49 | IoC就是 Inversion of Control,控制反轉。 |
| 50 | |
| 51 | 在Java開發過程中,IoC意謂著將你設計好的類別交給系統去控制,而不是在你的類別內部自己控制。 |
| 52 | |
| 53 | 實現IoC有兩種方式:Dependency Injection與Service Locator, |
| 54 | |
| 55 | Spring 所採用的是Dependency Injection 來實現 IoC,中文翻譯為依賴注入,依賴注入的意義是:「保留抽象介面,讓組件依賴於抽象介面,當組件要與其它實際的物件發生依賴關係時,藉過抽象介面來注入依賴的實際物件。」 |
52 | 56 | |
53 | 57 | 看看下面這個程式: |
… |
… |
|
65 | 69 | } |
66 | 70 | }}} |
| 71 | |
67 | 72 | BusinessObject 依賴於實際的 FloppyWriter,為了讓 BusinessObject 獲得重用性,不讓 BusinessObject 直接依賴於實際的 FloppyWriter,而是依賴於抽象的介面: |
68 | 73 | |
… |
… |
|
102 | 107 | |
103 | 108 | 如果今天BusinessObject想要與UseDiskWriter物件發生依賴關係,可以這麼建立: |
| 109 | |
| 110 | {{{ |
| 111 | #!java |
104 | 112 | businessObject.setDeviceWriter(new UsbDiskWriter()); |
105 | | |
| 113 | }}} |
106 | 114 | |
107 | 115 | 由於BusinessObject依賴於抽象介面,在需要建立依賴關係時,可以透過抽象介面注入依賴的實際物件。 |
… |
… |
|
211 | 219 | 上面的BusinessObject所實現的是Type 2 IoC,透過Setter注入依賴關係, |
212 | 220 | |
213 | | == Type 3 IoC : Constructor injection== |
| 221 | == Type 3 IoC : Constructor injection == |
214 | 222 | |
215 | 223 | Type 3 IoC,則在是建構式上注入依賴關係,例如: |