Helper和Utility类有什么区别?

时间:2022-09-25 12:23:49

How determine how call a class XHelper or XUtils ?

如何确定如何调用类XHelper或XUtils?

To my mind :

在我心里 :

Helper class, is a class that can be instantiate and do some business work

Helper类,是一个可以实例化并做一些业务工作的类

Utils class, is a static class that perform small and repetitive operations on a kind of instance (example of utils classes ArrayUtils or IOUtils from Apache)

Utils类是一个静态类,它在一种实例上执行小的重复操作(来自Apache的utils类ArrayUtils或IOUtils的例子)

3 个解决方案

#1


63  

There are many naming styles to use. I would suggest Utils just because its more common.

有许多命名样式可供使用。我建议Utils只是因为它更常见。

A Utility class is understood to only have static methods and be stateless. You would not create an instance of such a class.

实用程序类被理解为只具有静态方法并且是无状态的。您不会创建此类的实例。

A Helper can be a utility class or it can be stateful or require an instance be created. I would avoid this if possible.

帮助程序可以是实用程序类,也可以是有状态的,或者需要创建实例。如果可能,我会避免这种情况。

If you can make the name more specific. e.g. if it has sorting methods, make it XSorter

如果你可以使名称更具体。例如如果它有排序方法,请将其设为XSorter

For arrays you can find helper classes like

对于数组,您可以找到辅助类

Array
Arrays
ArrayUtil
ArrayUtils
ArrayHelper

BTW a short hand for a utility class is an enum with no instances

BTW实用程序类的简写是没有实例的枚举

enum XUtils {;
    static methods here
}

If you need to implement an interface, I would use a stateless Singleton.

如果你需要实现一个接口,我会使用一个无状态的Singleton。

enum XHelper implements RequiredInterface {
   INSTANCE;
   // no instance fields.
}

#2


8  

In general? It's entirely arbitrary. There are no rules for this.

一般来说?这完全是武断的。对此没有规定。

#3


2  

There's no ultimate answer for this. Figure out one naming scheme and stick with it. Naming your packages and classes is an important part of software architecture, and nobody can take that decision away from you.

对此没有最终答案。找出一个命名方案并坚持下去。命名您的包和类是软件架构的重要组成部分,没有人可以从您那里做出决定。

I personally like the XHelper better, but I see XUtils more often in foreign code.

我个人更喜欢XHelper,但我在外国代码中经常看到XUtils。

I also like the "plural" naming scheme you will find both in the JDK and Guava:

我也喜欢你在JDK和Guava中找到的“复数”命名方案:

if a class deals with Collection objects, it's called Collections

如果一个类处理Collection对象,则称为Collections

Array > Arrays (jdk)
List > Lists (guava)
Map > Maps (guava)

数组>数组(jdk)列表>列表(番石榴)地图>地图(番石榴)

etc.

等等

#1


63  

There are many naming styles to use. I would suggest Utils just because its more common.

有许多命名样式可供使用。我建议Utils只是因为它更常见。

A Utility class is understood to only have static methods and be stateless. You would not create an instance of such a class.

实用程序类被理解为只具有静态方法并且是无状态的。您不会创建此类的实例。

A Helper can be a utility class or it can be stateful or require an instance be created. I would avoid this if possible.

帮助程序可以是实用程序类,也可以是有状态的,或者需要创建实例。如果可能,我会避免这种情况。

If you can make the name more specific. e.g. if it has sorting methods, make it XSorter

如果你可以使名称更具体。例如如果它有排序方法,请将其设为XSorter

For arrays you can find helper classes like

对于数组,您可以找到辅助类

Array
Arrays
ArrayUtil
ArrayUtils
ArrayHelper

BTW a short hand for a utility class is an enum with no instances

BTW实用程序类的简写是没有实例的枚举

enum XUtils {;
    static methods here
}

If you need to implement an interface, I would use a stateless Singleton.

如果你需要实现一个接口,我会使用一个无状态的Singleton。

enum XHelper implements RequiredInterface {
   INSTANCE;
   // no instance fields.
}

#2


8  

In general? It's entirely arbitrary. There are no rules for this.

一般来说?这完全是武断的。对此没有规定。

#3


2  

There's no ultimate answer for this. Figure out one naming scheme and stick with it. Naming your packages and classes is an important part of software architecture, and nobody can take that decision away from you.

对此没有最终答案。找出一个命名方案并坚持下去。命名您的包和类是软件架构的重要组成部分,没有人可以从您那里做出决定。

I personally like the XHelper better, but I see XUtils more often in foreign code.

我个人更喜欢XHelper,但我在外国代码中经常看到XUtils。

I also like the "plural" naming scheme you will find both in the JDK and Guava:

我也喜欢你在JDK和Guava中找到的“复数”命名方案:

if a class deals with Collection objects, it's called Collections

如果一个类处理Collection对象,则称为Collections

Array > Arrays (jdk)
List > Lists (guava)
Map > Maps (guava)

数组>数组(jdk)列表>列表(番石榴)地图>地图(番石榴)

etc.

等等