简体   繁体   中英

Should I create static method or abstract superclass

I am trying to refactor a project in which there are same methods which are spread across various classes. To reduce code duplication, should I move the common code to an abstract superclass or should I put it in a static method in a utility class?

EDIT Some of the methods are for generic stuff which I believe can be made static. While there are others which refer to attributes of the class, in which case I think it makes more sense to make it as an abstract super class.

Well, I follow a rule: Don't use base class to remove code duplication, use utility class.

For inheritance, ask question to yourself: Does Is-A relationship exist?

Another rule, which most of the times is correct, is: Prefer composition over inheritance

using static utility class is NOT true composition but it can be called a derivation of it.

Apply these rules to your secenrios and take a decision keeping in mind maintanence and scalability. However it will be good if you could add more details to your quesiton.

It depends on what your code is doing. Are they utility methods? Are they specific/specialized class methods? Is this a heavy multithreaded application?

Keep in mind that if you make them static and your application is multithreaded, you will have to protect them w locks. This, in turn, reduces concurrency. In this case, depending on how many threads call that same piece of code, you might consider moving it (the code) to a super class.

Another point to consider may be the type of work these functions do. If that is scattered, you should create a facade / helper / util class with static methods.

As others have mentioned the answer to this depends on the context of the problem and the duplicated code.

Some things to consider

  • Does the duplicated code mutate the instance of the object. In this case a protected method in a common abstract class
  • Instead of Static utility class consider a singleton, Static methods can be problematic for pure unit testing although testing frameworks are getting better at this.
  • Inheritance can be tricky to get right, think about if these objects from the different classes are really related and require some OO re-factoring ? or are they disjoint pieces of domain logic that happen to require similar bits of code.

If it does not use any class members you might do it static!

But you should do it in a abstract class or mother class

If the methods use many fields or methods of the class they should not be static. If they are something that a subclass might want to modify they should not be static. If the methods should be part of an Interface they cannot be static.

Otherwise it's your call and you will probably change your mind later. :-)

At first glance, I would say that it would be better to make the common code as a public static method in a public class. This will make the method useful to any class just by using

    UtilityClassName.methodName();

This is better then making it a concrete method in an abstract super-class because then you will always need to extend this super-class in all the classes where you want to use this one single method.

But now, as you said that the method's behavior depends on some variables. Now, if it depends on the instance variables of different classes, then better add this method in an interface and let all your classes implement this interface and have their own implementation of the same.

But again if these variables are constant values, then have these constant values in an interface. Implement these interface in your utility class. And again make it a static method in that utility class which will directly use these constants.

For eg Consider foll. common code of returning area of a circle.

    public interface TwoDimensional{
        double PI = 3.14;
    }

    public class MyUtility implements TwoDimensional{
        public static double getCircleArea(double radius){
            return PI*radius*radius;
        } 
    }

Here, you can see that method getCircleArea() depends on the radius which will be different for different classes but still I can pass this value to the static method of myUtility class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM