Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

20
Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection Shih-Peng Lin 2013/12/6

description

A brief introduction to what is Inversion of Control, Dependency Inversion Principle, and Dependency Injection.

Transcript of Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

Page 1: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

Inversion of Control vs.Dependency Inversion Principle vs.

Dependency Injection

Shih-Peng Lin2013/12/6

Page 2: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

What is Inversion of Control?

• 一種軟體設計技巧;透過在執行時期才將必須共同運作的軟體元件組合起來執行工作,以降低程式碼間的 coupling 與 cohesion 。

• 名詞來源– 最早提到類似此概念的文章為 1983 年出刊的“ The

Mesa Programming Environment” 一文。• 當時稱為“ Hollywood Principle”

– 1988 年的“ Designing Reusable Classes” 則是已知最早使用 Inversion of Control 此一名詞與對應概念的文獻。

Page 3: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

What is Inversion of Control?

• 只要是符合這項描述的軟體設計模式,都能稱為 Inversion of Control :– “ 透過在執行時期才將必須共同運作的軟體元

件組合起來執行工作,以降低程式碼間coupling 與 cohesion 的軟體設計技巧”

Page 4: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

Code in control

Page 5: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

Code in inverted control(Control in windowing system)

Page 6: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

More examples

• Behavior of many frameworks– Spring framework 、 Android framework 、 EJB

framework 。• Implementation techniques– Dependency injection 、– Factory pattern 、– Template pattern 、– Strategy pattern...etc 。

Page 7: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

In other words...

• Inversion of Control 基本上是個高階的抽象 ( 不是抽象化 ) 概念與原則,在現代軟體開發環境中處處可見其蹤影。

Page 8: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

What is Dependency Inversion Principle?

• 由 Robert C. Martin 所定義:– High-level modules should not depend on low-

level modules. Both should depend on abstractions.

– Abstractions should not depend upon details. Details should depend upon abstractions.

• 一種透過使高階元件與低階元件以抽象層協同工作,以降低程式碼耦合度的技巧。

Page 9: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

Bad design caused by high coupling

• Rigidity– 難以對程式碼做更動;因為每一個變動都會影

響到系統中的太多地方。• Fragility– 當你做了一項變動,系統中某個意外的部份很

容易毀損。• Immobility – 元件很難以在另一個程式中再利用,因為其中

有太多與原程式纏繞的實作部分。

Page 10: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

High efferent coupling

Page 11: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

Dependency Inversion

Page 12: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

More examples

• Plugin mechanism• Dependency injection• Adapter pattern

Page 13: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

What is Dependency Injection?

• 一種軟體設計模式;移除程式碼中 hard-coded 的相依性,使該相依性元件能在執行時期動態地被給予 ( 注入 ) 。

• 是 Dependency Inversion Principle 的一種實作方法。

Page 14: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

Highly coupled dependency

Page 15: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

Injected dependency (Manually)

Page 16: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

Injected dependency (Automatically)

Page 17: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

優缺點• 優點– 提供使用元件時的彈性;

• 這對 unit test 來說非常有用,因其可以更容易地注入其他元件的 fake implementation

– 有效隔離元件,讓元件能更符合 single responsibility等原則。

• 缺點– 隱藏了相依性的細節,讓執行時期錯誤的發生機率增

加,而無法在編譯時期偵測到。– 由於相依性的隱藏,被注入的元件若做了大幅度的改

變,其使用者必須盡早得知以做出對應修改。

Page 18: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

總結• Inversion of Control 是一個高階的抽象概念。– 不是一個關於“抽象化”的概念,而僅僅是個“抽象的”

概念而已。

• Dependency Inversion Principle 是在某些情況下達成IoC的手段。– 我們剛好可以把某些元件的行為抽出成為介面來描述的時

候。

• Dependency Injection 則是在另一些情況下完成 DIP更具體的實作方式。– 剛好有某些元件很適合在執行時期才決定要使用的型別時。

Page 19: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

所以 ...

• 相依性注入就是相依性反轉 X• 相依性反轉就是控制反轉 X• 相依性注入能達到控制反轉的效果 O• Spring framework (or Grails) 具有控制反轉機

制 O• Android framework 裡元件的生命週期

callback methods 是控制反轉的一種呈現 O

Page 20: Inversion of Control vs. Dependency Inversion Principle vs. Dependency Injection

References

• Martin Fowler (2005). InversionOfControl.• Martin Fowler (2004). Inversion of Control Containers and

the Dependency Injection pattern.• Robert C. Martin (1996). The Dependency Inversion

Principle.• Kyle Baley and Donald Belcham (2010). Brownfield

Application Development in .NET.• Inversion of control. In Wikipedia. Retrieved Dec. 5 2013.• Dependency inversion principle. In Wikipedia. Retrieved

Dec. 5 2013.• Dependency injection. In Wikipedia. Retrieved Dec. 5 2013.