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?
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.
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.
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.
Any method of a class which is not static is called non-static method or an instance method.
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:
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.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.I hope this helps you out a bit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With