Should Helper/Utility Classes be abstract?

前端 未结 11 1950
时光说笑
时光说笑 2020-12-13 08:38

I commonly find myself extracting common behavior out of classes into helper/utility classes that contain nothing but a set of static methods. I\'ve often wondered if I sho

相关标签:
11条回答
  • 2020-12-13 08:55

    someone mentioned that in C# 3.0 you could accomplish this via extension methods. I'm not a C# guy, did some back in the 1.5/2.0 days, but have not used it since then. Based on a very cursory understanding I think something similar can be accomplished in java with static imports. I realize its not at all the same thing, but if the goal is to just make these utility methods seem a bit more "native"(for lack of a better term) to the calling class, I think it will do the trick. Assuming the Utilities class I declared in my original question.

    import static Utilities.getSomeData;
    
    public class Consumer {
    
        public void doSomething(){
    
            String data =  getSomeData();
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 09:00

    You could just declare a private constructor that does nothing.

    The problem with declaring the class "abstract" is that the abstract keyword usually means that class is intended to be subclassed and extended. That's definitely not what you want here.

    0 讨论(0)
  • 2020-12-13 09:00

    No, but if your language supports it, there's a strong argument to be made that in most cases they should (can) be declared as 'static'... Static tells the compiler that they cannot be instantiated, and that all methods in them must be static.

    Abstract is for classes that DO have instance-based implementation details, which WILL be used by instances of derived classes...

    0 讨论(0)
  • 2020-12-13 09:09

    Might I offer some constructive advice?

    If you are doing a lot of this, there are two problems you will run into.

    First of all, a static method that takes a parameter should often be a part of the object that is that parameter. I realize this doesn't help for objects like String, but if it takes objects you've defined, you could almost certainly improve the object by including your helper as a method of that object.

    If it takes all native values, you probably could define an object that it's a method of. See if you can find any grouping of those native values and group them as an object. If you just try that, you'll find a lot of other uses for that little mini-object, and before you know it it will be amazingly useful.

    Another thing, if you have a utility class with a bunch of semi-related static methods and static variables, you almost always want it to be a singleton. I found this out by trial and error, but when you find out you need more than 1 (eventually you will), it's MUCH easier to make a singleton into a multipleton(?) then to try to change a static class into a multipleton(okay, so I'm making words up now).

    Good luck. This stuff was mostly trial and error for me--figured it out like 5 years ago though, and I've never found an instance where I regretted not having static class/methods.

    0 讨论(0)
  • 2020-12-13 09:10

    Don't bother making them abstract, but include a private parameterless constructor to prevent them from ever being instantiated.

    Point of comparison for those interested: in C# you would declare the class to be static, making it abstract and sealed (Java's final) in the compiled form, and without any instance constructor at all. That also makes it a compile-time error to declare a parameter, variable, array etc of that type. Handy.

    0 讨论(0)
  • 2020-12-13 09:11

    I don't declare utility classes abstract, I declare them final and make the constructor private. That way they can't be subclassed and they can't be instantiated.

    
    
    public final class Utility
    {
        private Utility(){}
    
        public static void doSomethingUseful()
        {
            ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题