Methods as parameters

W

web02

I have a class X with methods that reside off in a server package:
public a(String);
public a(String);
public c(String);

In my program, I have a method M.

I want to call M passing it X.a, X,b or X.c with a string parameter.

I cannot find a way using interfaces and reflection is too slow (400K record
files)..

What is going on is a bit wierd. X gives me access to a legacy
database(ISAM). a, b and c are methods to put specific data items into the
database. If the value is blank, the store isn't to be done. So I thought
I would do something like

M(X.a, "Harry");
M(X.b, someValue);
M(X.c, "Truman");

Then M would check the length of the values and only do the call if length >
0. If someValue is empry, X.b would not be called.

Thanx,
Bill
 
L

Lew

web02 said:
I have a class X with methods that reside off in a server package:
public a(String);
public a(String);
public c(String);

In my program, I have a method M.

I want to call M passing it X.a, X,b or X.c with a string parameter.

I cannot find a way using interfaces and reflection is too slow (400K
record files)..

What is going on is a bit wierd. X gives me access to a legacy
database(ISAM). a, b and c are methods to put specific data items into
the database. If the value is blank, the store isn't to be done. So I
thought I would do something like

M(X.a, "Harry");
M(X.b, someValue);
M(X.c, "Truman");

Then M would check the length of the values and only do the call if
length > 0. If someValue is empry, X.b would not be called.

If I read you correctly, you are asking for something similar to "closures",
or "first-class functions", that is, functions as objects in their own right.

Java does not have this feature, yet, exactly. It can use (possibly
anonymous) classes instead, for most things. The idiom is wordier than
closure syntax, some might say ugly. On the bright side, everything you need
is jammed into the code in one place.

Here's how it works.

You need versions of a common supertype, or an enum, that know how to call
each method a(), b() and c(). (Using the parentheses helps us know that we're
talking about methods, not classes, variables or objects.) Let's call the
supertype "Doer" - the thing that runs the method we want to choose and takes
a String.

Here's an untested, not yet even compiled version:

public class Foo
{
private final X x = new X();

static interface Doer
{
public void do( String target );
}

private final Doer aDoer = new Doer ()
{
@Override public void do( String target )
{
x.a( target );
}
};

private final Doer bDoer = new Doer ()
{
@Override public void do( String target )
{
if ( target != null && target.length() > 0 )
{
x.b( target );
}
}
};

private final Doer cDoer = new Doer ()
{
@Override public void do( String target )
{
x.c( target );
}
};

public final void process( Datum datum )
{
Doer doer;
switch ( datum.getCondition() )
{
case DOA: doer = aDoer; break;
case DOB: doer = bDoer; break;
case DOC: doer = cDoer; break;
default: throw new IllegalStateException("No Condition!");
}
doer.do( datum.getTarget() );
}
}

An alternative form of this idiom has the enum Condition (implicit in the
above) carry the variant do() method.

datum.getCondition().do( datum.getTarget() );

Then you can look up the Condition based on its
static valueOf( Something thing ).

So the outside world provides a SomeThing instance, and your class instance calls

Condition.valueOf( datum.getSomeThing() ).do( datum.getTarget() );

This maps the rather non-object-oriented switch() idiom to the essential OO
idiom of polymorphism.
 

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,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top