Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Function calls...

Reply
Thread Tools

Function calls...

 
 
Raymond Martineau
Guest
Posts: n/a
 
      12-26-2004
In C, it's possible to create an array of the following data structure to
associate a function call with a specific key or identifier:

struct {
char *key;
void (*function)();
} a[] = { { "Function1", Function1} ,
{ "Function2", Function2}};

(Or some other varient that you are comfortable with.)

I would like to know the appropriate procedure on how to implement the same
in Java. I would prefer to use the above system, as it can be much quicker
to call a function by looking up the associated token in a hashtable as
opposed to writing many lines of an if/else ladder.

The first thought that appeared in my mind would involve reflecting
classes. Checking with the API spec indicated that it should work, but I'm
not sure that placing a large number of methods in one class is the best
solution.

In case you're wondering, I'm using this system to interpret a language
that uses a large number of keywords (with each keyword executing its own
function.)

 
Reply With Quote
 
 
 
 
Nicky
Guest
Posts: n/a
 
      12-26-2004
Hi

Reflection is not very fast.
Another solution is to create an abstract class with a single method:

public abstract class AbstractFunction {
public abstract void execute();
}

and to extend and implement that class for each function:

public class Function1 extends AbstractFunction {
public void execute() {
// Code for function1
}
}

Hope it helps
Nicky

"Raymond Martineau" <> wrote in message
news:...
> In C, it's possible to create an array of the following data structure to
> associate a function call with a specific key or identifier:
>
> struct {
> char *key;
> void (*function)();
> } a[] = { { "Function1", Function1} ,
> { "Function2", Function2}};
>
> (Or some other varient that you are comfortable with.)
>
> I would like to know the appropriate procedure on how to implement the
> same
> in Java. I would prefer to use the above system, as it can be much
> quicker
> to call a function by looking up the associated token in a hashtable as
> opposed to writing many lines of an if/else ladder.
>
> The first thought that appeared in my mind would involve reflecting
> classes. Checking with the API spec indicated that it should work, but
> I'm
> not sure that placing a large number of methods in one class is the best
> solution.
>
> In case you're wondering, I'm using this system to interpret a language
> that uses a large number of keywords (with each keyword executing its own
> function.)
>



 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      12-26-2004
"Nicky" <> wrote in message
news:...
> Hi
>
> Reflection is not very fast.
> Another solution is to create an abstract class with a single method:
>
> public abstract class AbstractFunction {
> public abstract void execute();
> }
>


Abstract classes should never be used as a pure virtual type (with only
abstract methods), or a concrete type (with no abstract methods).
It is arguable that abstract classes should be used at all (inheritance from
anything but an interface is pure evil, but that's another story for another
camp fire).

In the case of an attempt to use an abstract class as a pure virtual type as
has been suggested, an interface is always more suited.

--
Tony Morris
http://xdweb.net/~dibblego/



 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      12-26-2004

"Tony Morris" <> wrote in message
news:Xbvzd.88751$...
> "Nicky" <> wrote in message
> news:...
> > Hi
> >
> > Reflection is not very fast.
> > Another solution is to create an abstract class with a single method:
> >
> > public abstract class AbstractFunction {
> > public abstract void execute();
> > }
> >

>
> Abstract classes should never be used as a pure virtual type (with only
> abstract methods), or a concrete type (with no abstract methods).
> It is arguable that abstract classes should be used at all (inheritance

from
> anything but an interface is pure evil, but that's another story for

another
> camp fire).
>
> In the case of an attempt to use an abstract class as a pure virtual type

as
> has been suggested, an interface is always more suited.
>
> --
> Tony Morris
> http://xdweb.net/~dibblego/
>
>
>


It's worth nothing that a common web application framework, known as Struts
(struts.apache.org last I checked), makes use of this antipattern.
Struts is reknowned for its 'crustiness' for this, and many other, reasons.

--
Tony Morris
http://xdweb.net/~dibblego/


 
Reply With Quote
 
Lee Fesperman
Guest
Posts: n/a
 
      12-26-2004
Raymond Martineau wrote:
>
> In C, it's possible to create an array of the following data structure to
> associate a function call with a specific key or identifier:
>
> struct {
> char *key;
> void (*function)();
> } a[] = { { "Function1", Function1} ,
> { "Function2", Function2}};
>
> (Or some other varient that you are comfortable with.)
>
> I would like to know the appropriate procedure on how to implement the same
> in Java. I would prefer to use the above system, as it can be much quicker
> to call a function by looking up the associated token in a hashtable as
> opposed to writing many lines of an if/else ladder.
>
> The first thought that appeared in my mind would involve reflecting
> classes. Checking with the API spec indicated that it should work, but I'm
> not sure that placing a large number of methods in one class is the best
> solution.
>
> In case you're wondering, I'm using this system to interpret a language
> that uses a large number of keywords (with each keyword executing its own
> function.)


Reflection will work but is not the best choice here. In Java, you would normally use an
object reference instead of a function pointer. The object's class would implement an
interface containing the common method signature. This is quite efficient as opposed to
reflection and provides additional OO capabilities vs. a function pointer.

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
================================================== ============
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      12-26-2004
Raymond Martineau wrote:

> In case you're wondering, I'm using this system to interpret a language
> that uses a large number of keywords (with each keyword executing its own
> function.)


For such an application, I think that the best way to go would be to use lots
of little objects that all have different implementations of the same generic
method.

E.g (non-compliable stripped down pseudo-Java):

=====
abstract class Action { void doIt(); }
class Action1 extends Action { void doIt() { /* something */} }
class Action2 extends Action { void doIt() { /* something */} }
....

Map actions = new HashMap();

actions.add("Action1", new Action1());
actions.add("Action2", new Action2());
.....
=====

In practise I would probably use anonymous inner classes rather than explicitly
naming each subclass. The effect is identical, but it would help keep the
source looking somewhat tighter. (But also make it harder to see what's going
on, which is why I used explicit classes in the above example).

-- chris


 
Reply With Quote
 
Peter Sestoft
Guest
Posts: n/a
 
      12-26-2004
(Raymond Martineau) writes:

> In C, it's possible to create an array of the following data structure to
> associate a function call with a specific key or identifier:
>
> struct {
> char *key;
> void (*function)();
> } a[] = { { "Function1", Function1} ,
> { "Function2", Function2}};
>
> (Or some other varient that you are comfortable with.)
>
> I would like to know the appropriate procedure on how to implement the same
> in Java. I would prefer to use the above system, as it can be much quicker
> to call a function by looking up the associated token in a hashtable as
> opposed to writing many lines of an if/else ladder.


public interface Fun {
void call();
}
public Pair {
public final String key;
public final Fun fun;
public Fun(String key, Fun fun) {
this.key = key; this.fun = fun;
}
}

Pair[] a =
{ new Pair("Function1",
new Fun() {
public void call() { ... body of Function1 ... }
}),
new Pair("Function2",
new Fun() {
public void call() { ... body of Function2 ... }
}),
...
}

If you build a hash map the array like this:

Map<String,Fun> action = new HashMap<String,Fun>();
for (Pair p : a)
action.put(p.key, p.fun);

you get very fast execution of each instruction:

action.get(keyword).invoke();

Peter
 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      12-26-2004
Nicky wrote:
> Hi
>
> Reflection is not very fast.
> Another solution is to create an abstract class with a single method:
>
> public abstract class AbstractFunction {
> public abstract void execute();
> }
>
> and to extend and implement that class for each function:
>
> public class Function1 extends AbstractFunction {
> public void execute() {
> // Code for function1
> }
> }
>
> Hope it helps
> Nicky
>
> "Raymond Martineau" <> wrote in message
> news:...
>



Sipped

The usual name for this is 'Command pattern', however, like Tony said,
the AbstractFunction class should always be an interface.

So.

public interface Command {

void execute();

}

public class KeywordCommandTwo implements Command {


public void execute() {
...
}

}

public class KeywordCommandOne implements Command {


public void execute() {
...
}

}


And this code can be used like this....

public class Foo {

private Map keywordCommands = new HashMap();

public int main(String[] args) {


keywordCommands.put(keywordOne, new KeywordCommandOne());
keywordCommands.put(keywordTwo, new KeywordCommandTwo());


// load each keyword from file and execute its command.
for each keyword in file {
KeywordCommand command = (KeywordCommand)
keywordCOmmands.get(keyword);

command.execute();
}

}

}
 
Reply With Quote
 
Ryan Stewart
Guest
Posts: n/a
 
      12-26-2004
"Tony Morris" <> wrote in message
news:Xbvzd.88751$...
> Abstract classes should never be used as a pure virtual type (with only
> abstract methods), or a concrete type (with no abstract methods).
> It is arguable that abstract classes should be used at all (inheritance
> from
> anything but an interface is pure evil, but that's another story for
> another
> camp fire).
>

I'll bite. Why never use abstract classes?


 
Reply With Quote
 
Andrew McDonagh
Guest
Posts: n/a
 
      12-26-2004
Ryan Stewart wrote:
> "Tony Morris" <> wrote in message
> news:Xbvzd.88751$...
>
>>Abstract classes should never be used as a pure virtual type (with only
>>abstract methods), or a concrete type (with no abstract methods).
>>It is arguable that abstract classes should be used at all (inheritance
>>from
>>anything but an interface is pure evil, but that's another story for
>>another
>>camp fire).
>>

>
> I'll bite. Why never use abstract classes?
>
>


in Java, they are the 'same' as in they tell you that a given any
derived/implementing class has those particular methods and also can be
substituted when the interface/abstract class type is used as the
referencing type.

However, in java a class can only have one base class, but can implement
many different interfaces at the same time.

It comes down to cohesion and coupling.

In C++ pure virtual abstract classes are just about the same...(ignoring
the differences ;- )

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57