6

It seems the need for a type like the following would be so ubiquitous that something like it should be already built into Java:

public interface Executer<T> {
   void execute(T object);
}

It can then be used in other classes like this trivial example that calls a bunch of executers on an object.

class Handler<T> implements Executer<T> {
   List<Executer<T>> executerList;

   Handler(List<Executer<T>> executer) {
      this.executerList = executer;
   }

   void execute(T t) {
      for (Executer<T> executer : this.executerList) {
         executer.execute(t);
      }
   }
}

Is there a built-in type equivalent or a common library equivalent? Is there a name for this concept?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
  • 1
    What should implementations of your Executer interface do, given they have to be invoked on a specific type T. – wmorrison365 Jan 25 '12 at 17:20
  • 4
    Is `Runnable` close to what you're looking for? – aardvarkk Jan 25 '12 at 17:20
  • I'd agree that Runnable would be sufficient. I'm not really sure what your construct adds. There's also the concurrent Executor classes I suppose but these work off Runnable and exist to manage concurrent invocations. – wmorrison365 Jan 25 '12 at 17:22
  • @wmorrison365 implementations would perform an operation on a T object. – Jeff Axelrod Jan 25 '12 at 17:23
  • 1
    @aardvarkk Runnable's run() method doesn't accept a parameter. – Jeff Axelrod Jan 25 '12 at 17:24
  • @glenviewjeff I know, it'll do something with a T but you're going to have a TExecuter to do that (i.e. something that knows all about your T). What's the purpose for wrapping it? What does your handler add? – wmorrison365 Jan 25 '12 at 17:27
  • You would create an implementation of `Runnable` with a constructor and setters. You instantiate your `MyRunnable(mydata);` then allow the `Thread`, `Executor`, `Timer`, etc deal with it - i.e. execute it's `run` method. – wmorrison365 Jan 25 '12 at 17:28
  • @wmorrison365, it adds the ability to use dependency injection with Executers, allows separation of concerns, allows you to write classes that take lists of Executers to perform a list of tasks, etc. – Jeff Axelrod Jan 25 '12 at 17:29
  • @wmorrison365 I think you're misunderstanding. This has nothing to do with threading, only performing an action (i.e., a simple method call.) – Jeff Axelrod Jan 25 '12 at 17:31
  • 1
    Well, in short, I've not seen a framework resembling this in terms of providing the pattern. I can only think of java.util.concurrent.Executor but that deals with managing invocation of `Runnables` in a multi-threaded environment. `Runnable` also resembles this but, again, I don't think you're especially interested in a multi-threaded env. – wmorrison365 Jan 25 '12 at 17:35
  • @wmorrison365 Correct, this has nothing to do with threading or concurrency. See my revised sample code for a better use case. – Jeff Axelrod Jan 25 '12 at 17:38
  • 1
    Look here: http://stackoverflow.com/questions/3244582/java-equivalent-of-net-actiont-and-funct-u-etc – Jordão Jan 25 '12 at 18:09

2 Answers2

4

I think the name of the concept is the strategy pattern. Effectively you are encapsulating an algorithm, and this makes it easier to swap out one strategy for another or to apply a number of strategies.

This design pattern is very easy to implement, and the execute method need not take only a single argument of a specific type. As such, I doubt you will find a built-in Java type, but it is a well-known design pattern.

Michael McGowan
  • 6,528
  • 8
  • 42
  • 70
  • Yeah, or possibly the Command pattern. This would provide good separation of concerns. – wmorrison365 Jan 25 '12 at 17:43
  • Oh, of course it's the strategy pattern. I don't know why I didn't recognize it. I'm a bit surprised that this specific interface doesn't come up frequently enough to warrant a built-in type. – Jeff Axelrod Jan 25 '12 at 17:44
  • @glenviewjeff Well I think then you'd need one for Executor, one for Executor, one for Executor, etc. That might bet a bit strange to include as a built-in type...especially when defining the interface is so easy to do for your specific use-case. – Michael McGowan Jan 25 '12 at 17:46
  • That wouldn't be without [precedent](http://msdn.microsoft.com/en-us/library/bb549311.aspx)... although I agree with your point more than Microsoft's choice to only implement the and . – Jeff Axelrod Jan 25 '12 at 18:24
2

The closest I know of is Guava's Function<F,T>.

I don't think there's anything in the standard library that does exactly what you're asking. Runnable is somewhat close, but takes no arguments.

P.S. I think "function" or "functor" is exactly the right name for this concept.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I agree that while Guava's `Function` type could actually be substituted, my use doesn't fit `Function`'s intention. – Jeff Axelrod Jan 25 '12 at 17:45