Inject Util Class with Google Guice vs static Methods?

前端 未结 3 1968
小蘑菇
小蘑菇 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:46

    It depends on the nature of your convert() method.

    If it's something

    • simple
    • deterministic (i.e. doesn't depend on additional parameters)
    • have no side effects
    • is unlikely to change
    • etc

    you can keep it as a static utility method.

    Otherwise it's a good candidate for dependecy injection (you can rename it to ConversionService to make it more clear).

提交回复
热议问题