What are the differences between Helper and Utility classes?

后端 未结 5 388
小鲜肉
小鲜肉 2021-01-29 18:12

How determine how call a class XHelper or XUtils ?

To my mind :

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

相关标签:
5条回答
  • 2021-01-29 18:50

    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.

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

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

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

    etc.

    0 讨论(0)
  • 2021-01-29 18:51

    As Jesper said, it's entirely arbitrary. You can think of what works for your organization and make that the convention.

    For me, it's something like this:

    utils - Static class, that can be freely moved and imported anywhere.

    Doing general tasks that could be useful in different modules. As Peter Lawrey said, more specific names are useful.

    helper - Class helping another class or a module.

    Tasks that are only used in the module it's placed and won't make sense to be imported elsewhere. Hence the name could be more specific - ModuleNameHelper (e.g. AdministrationHelper, LoginHelper)

    0 讨论(0)
  • 2021-01-29 19:00

    A utility is a "leaf node" class of general use. That is, it doesn't have any dependencies into your project and can be ported from project to project without breaking or becoming useless. Examples: Vector3, RandomNumberGenerator, StringMatcher, etc...

    A "helper" seems to be any class whose design is to aid another class. These may or may not depend on your project. If you're creating a GameNetworkClient class, you could say the GameNetworkConnection class is a 'helper', because it "helps" the GameNetworkClient.

    The way developers refer to tools reflects common usage of these words. If you can recall hearing tools described as "helpful" vs "useful", a helpful tool tends to have some context (cheese grater helps to grate cheese, corn stripper helps to strip corn, speed loader helps to reload a firearm). A "utility" is expected to work in a variety of contexts (WD-40, duct tape, army-knives, glue, flashlight, etc...).

    0 讨论(0)
  • 2021-01-29 19:09

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

    0 讨论(0)
  • 2021-01-29 19:11

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

    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

    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

    enum XUtils {;
        static methods here
    }
    

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

    enum XHelper implements RequiredInterface {
       INSTANCE;
       // no instance fields.
    }
    
    0 讨论(0)
提交回复
热议问题