


Definition: An utility stores a group of related algorithms. It doesn't store any data. This is merely a way of organizing algorithms. Note: Utility is not a standard design pattern.
Usage: Use an utility when you have a group of related methods that are not complicated enough to each merit their own class.
class MathUtil {
static double convertToDouble(final int numerator, final int denominator) {
return (double)numerator / (double)denominator;
}
static int maximumInt(final int a, final int b) {
if(a >= b)
return a;
else
return b;
}
static int maximumIntFromList(final int... aNumber) {
int max = Integer.MIN_VALUE;
for(int number : aNumber)
if(number > max)
max = number;
return max;
}
static int minimumInt(final int a, final int b) {
if(a <= b)
return a;
else
return b;
}
static int minimumIntFromList(final int... aNumber) {
int min = Integer.MAX_VALUE;
for(int number : aNumber)
if(number < min)
min = number;
return min;
}
} //end class MathUtil
These methods are all related, so they are easier to access when they are all in one file. There's no particular reason to put them in separate classes.
Copyright (C) 2009 Steven Fletcher. All rights reserved.