Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to static Util Classes and Methods in Java

Tags:

java

oop

Say, I wanted to have this method which takes a Set and formats that set into a string according to some logic. Eg.

public String formatCustomerSet(final Set<Customer> customers) {
    String result = "";
    for (Customer customer : customers) {
        result += customer.getFirstName() + " : " + customer.getLastName() + "\n";
    }

    return result;
}

I am using this code in several classes. Now, where should I put this? I found that Utility classes should be avoided and should not be used? https://lostechies.com/chrismissal/2009/06/01/anti-patterns-and-worst-practices-utils-class/

Then, should it be Util class of a specific name like CustomerFormatter which is used, or should we make a OOP class out of it? Which one is recommended and why?

like image 768
hatellla Avatar asked Sep 23 '19 18:09

hatellla


People also ask

What can I use instead of static in Java?

You can use the full power of inheritance and overriding since your methods are no longer static. You can use the constructor to do any initialisation, including associating SQL with the table (SQL that your methods can use later). This should make all your problems above go away, or at least get much simpler.

What is alternative to static?

A common alternative is to use a singleton or an instance class where you have just one instance. This is preferable as it is easier to test, and inject different functionality. Use an enum class with no instances as a utility class.

Should util classes be static?

Pure utility classes should usually be static. When you have a class with well-defined input and output, no side effects and no state, then by definition it should be a static class.

What are non-static methods called?

Any method of a class which is not static is called non-static method or an instance method.


1 Answers

I disagree with the fact that util classes should be avoided. It is perfectly fine to have a class that contains static methods that are purely utilities. Sometimes functions or operations should just be functions or operations. There is not always a need to associate them with some abstract object type or whatever.

However, what I do agree with is that when making util classes, you should strictly define what types of utilities will be provided by the class. If you have a bunch of static methods that are related to doing mathematic operations, you can make a util class ExtendedMath. If you are doing a lot of mathematics and the class becomes too fat, try to refine it further. Group them by type of mathematics such as Algebra and Vector.

In the end, it is mostly just trying to use common sense. Does it make sense for a function to be part of an object, or can it just be an arbitrary operation?

In your case, you have to ask yourself the question: "Do I want to create a hard dependency of my code to the formatCustomerSet implementation?". If you know 100% for sure that you will always format customer sets in the exact same way, and you will never want different formatters then it might be okay to use a static method. If not, then maybe it's better to put it on a CustomerSetFormatter class as a non-static function.

I can't give you a yes or no answer, as you have to weight the pro's and the con's yourself. Just take the following things into account:

  • When you are referencing a static method, you are creating a hard dependency to that static method. Sometimes this is okay, sometimes it is not. A core principle of software development is reuse. Can you reuse the code that calls the static function in other contexts as well? Or is the static function too context-specific?
  • Another core principle in programming is dependency injection. Lets say you have Class A which requires a customer formatter. What you could do, is you can make a interface CustomerFormatter. In Class A you then define a constructor: A(CustomerFormatter formatter). What this allows you to do, is to reuse Class A while using different CustomerFormatter implementations. This greatly increases the reusability of Class A. When using static functions, this is no longer possible.
  • Common generic operations (such as the functions defined in Math, e.g. min(), max(), ceil() or floor()) are completely warranted to be static functions. They do one thing that is used commonly, and one thing only. Generally, you can take functions like that as an example of when a static method is perfectly warranted.
  • Is your function a pure function? This is a function that always gives the exact same output, for any unique input. Subsequent calls to the same functions will always yield the same result if the input stays the same. If this is the case, then it can be perfectly fine to make your method static. If this is not the case, and subsequent calls to the same function give different outputs depending on some arbitraty state or calls that were done previously, then you should consider making it non-static. Making static functions that provide different outputs for the same input because they depend on some state, is generally seen as an anti-pattern.

I hope this helps you out a bit.

like image 51
ImJustACowLol Avatar answered Sep 23 '22 02:09

ImJustACowLol