久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

靜態方法是 DI 反模式嗎?

Are static methods a DI anti-pattern?(靜態方法是 DI 反模式嗎?)
本文介紹了靜態方法是 DI 反模式嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我是一名 Java 開發人員,開始掌握依賴注入的全部威力,我突然意識到沒有辦法注入靜態方法.所以我開始思考:靜態方法是 DI 反模式嗎?

I am a Java developer who is beginning to grasp the full power of dependency injections, and it suddenly dawned on me that there's no way to inject a static method. So it got me thinking: are static methods DI anti-patterns?

更重要的是:如果我接受依賴注入,這是否意味著我需要停止編寫靜態方法?我問是因為在單元測試期間沒有辦法模擬它們并注入模擬靜態,這對我來說是一個巨大的關閉.

More importantly: if I were to embrace dependency injection, does this mean I need to stop coding static methods? I ask because there is no way to mock them and inject mock statics during unit tests, which is a huge turn-off for me.

編輯:我知道包裝"和注入現有靜態方法的常用方法是這樣的:

Edit: I know that a common way to "wrap" and inject an existing static method is like this:

public class Foo {
    public static void bar() { ... }
}

public interface FooWrapper {
    public void bar();
}

public class FooWrapperImpl implements FooWrapper {
    public void bar() {
        return Foo.bar();
    }
}

...但我不是在問如何注入現有的靜態方法...我是在問我是否應該完全停止編寫它們,如果我的所有代碼(從現在開始)都將接受這個概念DI.

...but I'm not asking how to inject an existing static method...I'm asking if I should stop writing them altogether, if all my code (from this point forward) is going to embrace the notion of DI.

此外,我看到很多與此類似的問題,但找不到提出相同問題的完全匹配項.如果您發現這確實是對另一個問題的欺騙,請指出給我,我將自己關閉這個問題(請不要只投票!).

Also, I see a lot of similarly-related questions to this, but couldn't find an exact match that asked this same question. If you see that it is indeed a dupe of another question, please point it out to me and I will close this question myself (please don't just closevote it!).

推薦答案

靜態方法適用于沒有關聯狀態的事物. 一些工廠方法,純函數式"方法,例如 Math.sin 等都是完全可以接受的靜態方法.java.lang.Mathjava.util.Collections 有許多完全可以接受的靜態方法的好例子.

Static methods are appropriate for things that don't have associated state. Some factory methods, "purely functional" methods like Math.sin, and the like are all perfectly acceptable static methods. java.lang.Math and java.util.Collections have many fine examples of perfectly acceptable static methods.

幸運的是,這些方法不需要依賴注入,也不需要與這些東西交互;它們并不難測試.他們沒有需要模擬或任何東西的依賴項.

Fortunately, these methods have no need for dependency injection, or to interact with such things; they're not unusually difficult to test. They don't have dependencies that would need mocking or anything.

另一方面,靜態或具有相關靜態的靜態方法是完全邪惡的.這一種反模式.

On the other hand, static state, or static methods with associated static state, are utterly evil. That is an anti-pattern.

當且僅當它總是在等效輸入上返回等效輸出時,通常有助于將方法定義為無狀態(因此是合法的靜態方法).這清楚地表明,例如數據庫查詢和文件系統 I/O 使方法成為有狀態的,因為它們的輸出會根據文件系統或數據庫中的內容而有所不同.

It frequently helps to define a method as being non-stateful (and therefore a legitimate static method) if, and only if, it always returns equivalent output on equivalent inputs. This makes it clear that e.g. database queries and filesystem I/O makes methods stateful, because their outputs will vary depending on what's in the filesystem or the database.

這篇關于靜態方法是 DI 反模式嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to mock super reference (on super class)?(如何模擬超級參考(在超級類上)?)
Java mock database connection(Java 模擬數據庫連接)
Mockito ClassCastException - A mock cannot be cast(Mockito ClassCastException - 無法投射模擬)
Set value to mocked object but get null(將值設置為模擬對象但獲取 null)
How to mock DriverManager.getConnection(...)?(如何模擬 DriverManager.getConnection(...)?)
Mockito; verify method was called with list, ignore order of elements in list(模擬;使用列表調用驗證方法,忽略列表中元素的順序)
主站蜘蛛池模板: 欧美一区二区三区四区视频 | 久久国产精品-国产精品 | 国产精品高潮呻吟久久 | 亚洲精品视频在线 | www国产亚洲精品久久网站 | 一级毛片大全免费播放 | 欧美精品一区三区 | 中文字幕精品一区二区三区在线 | 国产美女一区二区 | 久久男人| 成人免费在线网 | 在线天堂免费中文字幕视频 | www.色午夜.com | 亚洲国产成人在线视频 | 国产中文字幕在线观看 | 日韩免费视频 | 日日操操 | 国产高清在线精品一区二区三区 | 国产免费a | 精品av| 成人在线免费观看 | 久久免费精品视频 | 欧洲亚洲精品久久久久 | 中文字幕亚洲在线 | 黄色大片观看 | 国产午夜在线 | 欧美日韩中文在线观看 | 黄色一级免费看 | 日韩欧美国产精品综合嫩v 一区中文字幕 | 精品免费国产一区二区三区四区介绍 | 北条麻妃99精品青青久久 | www.99热| 91在线视频国产 | 久久国产一区 | 中文字幕av中文字幕 | 免费一二区 | 欧美国产亚洲一区二区 | 精品一区二区在线观看 | 成人二区| 在线高清免费观看视频 | 一区二区中文字幕 |