simple method to simulate function pointers in java

  • Thread starter Lino Ferrentino
  • Start date
L

Lino Ferrentino

Maybe there are other methods... I use this:

public final class b_callbacks {

private b_callbacks(){
}


public static interface void_f_void{
public void f();
}

public static interface void_f_int{
public void f(int i);
}


}

----

when I want to use a function pointer

import b_callbacks.void_f_int;

private void_f_int my_callback;

my_callback = new void_f_int(){
public void f(int i){
//code
}
};

to call the callback

my_callback.f(42);


====

For each function pointer type we create an interface.
 
M

Mayeul

Maybe there are other methods... I use this:

[Functor interfaces]

Well yes, that's pretty much it.
Note you do not have to declare these interfaces as nested in a class.
You may, if you think it makes matters clearer, but you don't have to.

public interface PettingStrategy {

void pet(Dog dog);

}

Note that it will usually lead to unclear terminology when you think in
terms of 'function pointers'. Function pointers are a general-purpose
language mechanism, which happen to not exist in Java. No need to pursue
it to the end.
Whenever you have a need for a mechanism of the sort, there is a
precise, well-defined, task you need to perform with this mechanism. You
should create interfaces designed to perform this task, and name these
interfaces and their methods accordingly. This will make your programs
clearer.
 
L

Lino Ferrentino

It will also make your code look and read more like idiomatic Java and
less like a literal translation of some other language.

ok... you are right. I was simply try to reduce Java to c.

So I needed function pointers, typedefs... and functions (not methods)
 
R

Roedy Green

see http://mindprod.com/jgloss/delegate.html

It is a verbose, somewhat more powerful technique than function
pointers.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
A

Arne Vajhøj

ok... you are right. I was simply try to reduce Java to c.

So I needed function pointers, typedefs... and functions (not methods)

Trying to program in language X using the paradigms of
language Y usually leads to bad code.

If you want to use language X then learn to do it the
X way.

Arne
 
A

Arne Vajhøj

Maybe there are other methods... I use this:

public final class b_callbacks {

private b_callbacks(){
}


public static interface void_f_void{
public void f();
}

public static interface void_f_int{
public void f(int i);
}


}

----

when I want to use a function pointer

import b_callbacks.void_f_int;

private void_f_int my_callback;

my_callback = new void_f_int(){
public void f(int i){
//code
}
};

to call the callback

my_callback.f(42);


====

For each function pointer type we create an interface.

It is perfectly valid.

You need to note two things:
1) the codes is not following Java coding convention
2) it is based on an assumption that all functions with
the same signature are interchangeable - that is not
very type safe

Arne
 
L

Lew

Arne said:
It is perfectly valid.

You need to note two things:
1) the codes is not following Java coding convention
2) it is based on an assumption that all functions with
the same signature are interchangeable - that is not
very type safe

The Java idiom is to use a functor, typically a single-abstract-method (SAM) interface, and pass an instance of that functor type to the object needing a callback or similar method. The object implementing the functor interface is the called-back object.

And as Arne says, do follow the coding conventions. Your code was nearly impenetrable. You name things by purpose in Java, not by implementation type.

Roughly (very roughly)_speaking, the functor pattern is something like:

public interface Updater
{
public void update( Information info );
}

============

public class SomethingThatUpdates
{
private final List <Updater> updaters = new ArrayList <> ();

public void addUpdater( Updater updater )
{
updaters.add( updater );
}

public void doUpdates()
{
Information info = obtainInformationSomehow();
for( Updater upd : updaters )
{
upd.update( info );
}
}

// etc.
}

============

public class SomethingToUpdate implements Updater
{
public void init( SomethingThatUpdates something )
{
something.addUpdater( this );
}

public void update( Information info )
{
// do something useful with 'info'
}

// etc.
}

============
 
J

John B. Matthews

<39f8a144-5eaa-4b7f-b335-efb75539ff95@glegroupsg2000goo.googlegroups.com
Lew said:
Arne said:
Lino said:
Maybe there are other methods... I use this:

[code omitted]

to call the callback

my_callback.f(42);


For each function pointer type we create an interface.

It is perfectly valid.

You need to note two things:
1) the codes is not following Java coding convention
2) it is based on an assumption that all functions with
the same signature are interchangeable - that is not
very type safe

The Java idiom is to use a functor, typically a
single-abstract-method (SAM) interface, and pass an instance of that
functor type to the object needing a callback or similar method. The
object implementing the functor interface is the called-back object.

And as Arne says, do follow the coding conventions. Your code was
nearly impenetrable. You name things by purpose in Java, not by
implementation type.

Roughly (very roughly)_speaking, the functor pattern is something like:

[code omitted]

A related example of this widely used pattern may be found in
`EventListenerList` [1], which adds a measure of type safety by
using a class literal as a runtime type-token [2].

[1]<http://download.oracle.com/javase/7/docs/api/javax/swing/event/EventListenerList.html>
[2]<http://download.oracle.com/javase/tutorial/extra/generics/literals.html>
 

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,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top