Helper和Utility類有什么區別?

[英]What are the differences between Helper and Utility classes?


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.

等等


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/08/30/72573872be3f537d40a97bfa8e12e4c2.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com