[英]Why did Joshua Bloch use 2*size + 1 for resizing the stack in Effective Java?
[英]Did Joshua Bloch say not to use singleton with underlying resources?
我一直在閱讀 Joshua Bloch 的Effective Java書。 In item #5, he seems to say not to use a singleton or static utility class to implement a class that depends on one or more underlying resources, and do not have the class create these resources directly, use dependency injection.
我想澄清一下他在這里試圖給出的確切建議。 當我有一些影響 Z2AB61A2DEB22ED4F8 的變量時,我是否應該根本不制作 singleton 或 static 實用程序 class(並且只使用一個類)? 或者我可以使用它,但只需要使用依賴注入? 這些策略中的哪一個最符合布洛赫的建議?
Joshua 談論的類是通往數據庫、文件系統或各種網絡資源等資源的網關。 它們很難用替代實現來代替。 如果您只使用常規類,可能實現一個抽象接口,您可以在任何需要它們的地方注入它們,並通過注入其他東西來替換它們。 這種替代實現的主要示例是在您不想訪問實際底層資源的單元測試中使用的 Mocks/Stubs/Fakes。 其他示例:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.