Inject Util Class with Google Guice vs static Methods?

前端 未结 3 1948
小蘑菇
小蘑菇 2020-12-31 17:22

I\'m wondering if it is a good style to inject utility methods with google guice.

Let\'s say we have a Converter Utility Class:

public class UtilClas         


        
3条回答
  •  礼貌的吻别
    2020-12-31 17:27

    Personally I typically try to not let the fact my application is wired using a dependency injection framework influence the design decisions I make for my classes etc. Good design practices should dictate that. In your case it would seem your utility class has no dependency on state therefore it seems a sensible candidate to remain as static.

    Regardless of this, your utility class is not an implementation of any interface so the client code using it still has a tightly coupled dependency. With this it is difficult to see what is the benefit of using DI to inject the utility class at all, as opposed to just referencing the static class directly in your client code.

提交回复
热议问题