静态方法是DI反模式吗? [英] Are static methods a DI anti-pattern?

查看:328
本文介绍了静态方法是DI反模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Java开发人员,开始使用来掌握依赖注入的全部功能,而且我突然觉得没有办法注入静态方法。所以它让我想到:是静态方法DI反模式?



更重要的是:如果我要拥抱依赖注入,这是否意味着我需要停止编码静态方法?我问,因为没有办法模拟他们,在单元测试中注入模拟静态,这对我来说是一个巨大的关闭。



修改:我知道一个常见的包装方法并注入一个现有的静态方法就是这样的:

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

public interface FooWrapper {
public void bar();
}

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

...但我不是问如何注入一个现有的静态方法...我问我是否应该停止写它们,如果我所有的代码(从这一点开始)将接受DI的概念。



另外,我看到很多类似相关的问题,但是找不到一个完全匹配的问题。如果你看到它确实是另一个问题的笨蛋,请指出我,我将自己关闭这个问题(请不要仅仅关闭它)。

解决方案

静态方法适用于没有关联状态的东西。一些工厂方法,纯功能方法,如 Math .sin 等都是完全可以接受的静态方法。 java.lang.Math java.util.Collections 有许多完美可接受的静态方法的很好的例子。 >

幸运的是,这些方法不需要依赖注入,或者与这些事情进行交互;他们不是很难测试。他们没有需要嘲弄或任何东西的依赖。



另一方面,静态或具有关联静态的静态方法是完全邪恶的。



它经常有助于将方法定义为非有状态(因此是合法的静态方法),如果,并且只有当它总是在等效输入上返回等效输出。这使得清楚的是,数据库查询和文件系统I / O使方法处于状态,因为它们的输出将取决于文件系统或数据库中的内容。


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();
    }
}

...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!).

解决方案

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.

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反模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆