整理Effective Java書中Item 5: Prefer dependency injection to hardwiring resources心得筆記

主旨

這篇在呼應item3提到的singleton以及item4中utils class的問題,建議使用依賴注入(dependency injection)。

點出問題

Static utility classes and singletons are inappropriate for classes whose behavior is parameterized by an underlying resource.

Static utility classes and singletons這兩種不適合使用參數取得底層資源,舉個白話一點的例子,今天需求是驗證信用卡卡號是否合法,你寫了一個utils class也遵照了item4提出的建議,很快地做完了。