Should I create a static method or abstract superclass

I am trying to reorganize a project in which the same methods exist that are distributed across different classes. To reduce code duplication, should I move the general 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 general things that I believe can be made static. Although there are others that relate to class attributes, in which case I think it makes sense to make it an abstract superclass.

+7
source share
7 answers

Well, I follow the rule: Do not use the base class to remove duplication of code, use the utility class.

To inherit, ask yourself: Is there an Is-A relationship?

Another rule, which in most cases is correct: Prefers composition over inheritance

using a static utility class is not a true composition, but it can be called its derivative.

Apply these rules to your sequenrios and make decisions based on motherhood and scalability. However, it will be nice if you can add more information to your quesiton.

+5
source

It depends on what your code does. Are they useful 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 the same piece of code calls, you might consider moving it (the code) to the superclass.

+2
source

Another issue to consider is the type of work performed by these functions. If this is scattered, you should create a facade / helper / util class with static methods.

+1
source

As already mentioned, the answer to this question depends on the context of the problem and the duplicated code.

Some things to consider

  • Whether duplicated code repeats an instance of an object. In this case, a protected method in a generic abstract class
  • Instead of the static utility class, consider a singleton method; static methods can be problematic for pure unit testing, although the test framework is getting better.
  • Inheritance can be difficult to make the right choice, consider whether these objects from different classes are really connected and require some kind of re-factoring OO? or they are disjoint parts of the domain logic that require similar bits of code.
+1
source

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

But you have to do it in an abstract class or mom's class

0
source

If methods use many fields or class methods, they should not be static. If this is what the subclass might want to change, they should not be static. If methods should be part of an interface, they cannot be static.

Otherwise, this is your call, and you will probably change your mind later. :-)

0
source

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

UtilityClassName.methodName(); 

This is better than making a specific abstract superclass method, because then you always need to extend that superclass in all classes where you want to use this single method.

But now, as you said, the behavior of a method depends on some variables. Now, if it depends on the instance variables of different classes, it is better to add this method to the interface and let all your classes implement this interface and have their own implementation of the same.

But then again, if these variables are constant values, then these constant values ​​are in the interface. Deploy this interface in your class. And again, make it a static method in this utility class that will directly use these constants.

For example, consider foll. common code of the returned area of ​​the 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 the getCircleArea () method depends on the radius, which will differ for different classes, but still I can pass this value to the static method of the myUtility class.

0
source

All Articles