"pointing" to functions to execute for scaleable code

M

metaperl

I've been a programmer for many years and am just learning Java. In
the code below, I think that the for loop from
j=0 to j=3 is poor code. Instead there should be an array of
"pointers" to each getInstance function and one should iterate through
that "dereferencing" each function pointer.

Pseudo code -

FunctionPointer fplist[] = { getInstance, getIntegerInstance,
getCurrencyInstance, getPercentInstance } ;

for (fp : fplist) {
fp->(locales)
}


import java.text.DecimalFormat;
import java.text.NumberFormat;

import java.text.ParseException;

import java.util.Locale;


class nf {
public static void main(String[] arg) {

// Print out a number using the localized number, integer, currency,
// and percent format for each locale
Locale[] locales = NumberFormat.getAvailableLocales();
double myNumber = -1234.56;
NumberFormat form;
for (int j=0; j<4; ++j) {
System.out.println("FORMAT");
for (int i = 0; i < locales.length; ++i) {
if (locales.getCountry().length() == 0) {
continue; // Skip language-only locales
}
System.out.print(locales.getDisplayName());
switch (j) {
case 0:
form = NumberFormat.getInstance(locales); break;
case 1:
form = NumberFormat.getIntegerInstance(locales); break;
case 2:
form = NumberFormat.getCurrencyInstance(locales); break;
default:
form = NumberFormat.getPercentInstance(locales); break;
}
if (form instanceof DecimalFormat) {
System.out.print(": " + ((DecimalFormat) form).toPattern());
}
System.out.print(" -> " + form.format(myNumber));
try {
System.out.println(" -> " + form.parse(form.format(myNumber)));
} catch (ParseException e) {}
}
}
}
}
 
E

Eric Sosman

metaperl said:
I've been a programmer for many years and am just learning Java. In
the code below, I think that the for loop from
j=0 to j=3 is poor code. Instead there should be an array of
"pointers" to each getInstance function and one should iterate through
that "dereferencing" each function pointer.
[code snipped]

Are you just stating an opinion, or do you have a question?
 
T

Tom Hawtin

metaperl said:
I've been a programmer for many years and am just learning Java. In
the code below, I think that the for loop from
j=0 to j=3 is poor code. Instead there should be an array of
"pointers" to each getInstance function and one should iterate through
that "dereferencing" each function pointer.

Being firmly in the object camp, Java does not have function pointers.
If you want something it should be an object. That object may be of a
strategy type that just adds one method and no state (other than
implementation class, identity and monitor).

The usual way of doing it is with an (anonymous) inner class. Since 1.5,
there has been an easy way to define a closed family, using enum.

enum NumberFormatType {
GENERAL() {
public NumberFormat get(Locale locale) {
return NumberFormat.getInstance(locale);
}
},
INTEGER() {
public NumberFormat get(Locale locale) {
return NumberFormat.getIntegerInstance(locale);
}
},
CURRENCY() {
public NumberFormat get(Locale locale) {
return NumberFormat.getCurrencyInstance(locale);
}
},
PERCENT() {
public NumberFormat get(Locale locale) {
return NumberFormat.getPercentInstance(locale);
}
};
public abstract NumberFormat get(Locale locale);
}
....
private final List<NumberFormatType> formatTypes = Arrays.asList(
NumberFormatType.GENERAL,
NumberFormatType.INTEGER,
NumberFormatType.CURRENCY,
NumberFormatType.PERCENT
);
...
for (NumberFormatType formatType : formatTypes) {
System.out.println("FORMAT");
for (Locale locale : locales) {
if ("".equals(locale.getCountry())) {
continue; // Skip language-only locales
}
System.out.print(locale.getDisplayName());
NumberFormat format = formatType.get(locale);
...

Tom Hawtin
 
L

Lew

Tom said:
Being firmly in the object camp, Java does not have function pointers.
If you want something it should be an object. That object may be of a
strategy type that just adds one method and no state (other than
implementation class, identity and monitor).

The usual way of doing it is with an (anonymous) inner class. Since 1.5,
there has been an easy way to define a closed family, using enum.

Not always an inner class. It is also common to use an interface, e.g.,
Runnable, and pass around different instances of implementations that may or
may not be inner classes.

A related term is "polymorphism", whereby a subclass object performs an action
declared by the superclass, but in its own way.

-- Lew
 
C

Chris Uppal

Tom said:
private final List<NumberFormatType> formatTypes = Arrays.asList(
NumberFormatType.GENERAL,
NumberFormatType.INTEGER,
NumberFormatType.CURRENCY,
NumberFormatType.PERCENT
);
...
for (NumberFormatType formatType : formatTypes) {

You could replace the above code with:

for (NumberFormatType formatType : NumberFormatType.values()) {

Seems simpler to me, though it might have made the meaning more obscure for the
OP.


I like this way of using enums, but I'd guess that most Java programmers would
see anonymous inner classes as more idiomatic for this example. More flexible
too, although that is not needed here.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top