0

Is there a simple Interface in Java like the following code?

public interface Delegate {
    void action();
}

Java Delegates? describes the functionality well. It's not a Supplier, not a Consumer, not a Function, not a Runnable(no async stuff needed), I just want to pass a lambda to a method to be executed in between common parts.

Now I'm wondering why I have to define this interface myself. Am I just unable to find the standard Java interface for this or am I missing some vital drawback here?

Usage in my code (simplified):

public void transferX(Xrequest request){
  transfer(request, () -> this.typedWrite(request));
}
public void transferY(Yrequest request){
  transfer(request, () -> this.typedWrite(request));
}
void transfer(BaseRequest request, final Delegate writeFunction){
  ...
  try{
    writeFunction.action();
    ...
  catch(...){...}
}
void typedWrite(Xrequest request){...}
void typedWrite(Yrequest request){...}
Thomas
  • 624
  • 11
  • 28

1 Answers1

1

OK, as Sweeper pointed out, that is just the Runnable interface. I just don't like the naming, since I immediately associate it with multithreading.

For comparison:

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}
Thomas
  • 624
  • 11
  • 28
  • 1
    Yeah, the naming comes from before the introduction of functional interfaces, so they kept it. – daniu Jul 30 '21 at 07:52