What are the differences between Helper and Utility classes?

后端 未结 5 387
小鲜肉
小鲜肉 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 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.
    }
    

提交回复
热议问题