Search code examples
javadesign-patternscommand-pattern

Interfaces for Command Pattern in Java


Is there a library out there that already provides the interfaces we need for the command pattern in Java?

For example:

 public interface Func1<T,R> { public R execute(T input); }

 public interface Func2<T1,T2,R> { public R execute(T1 input1, T2 input2); }

 public interface Predicate1<T> { public boolean execute(T input); }

 ....

Thanks.


Solution

  • Guava has your first and third interfaces (called Function and Predicate). Your second one, IMHO, is not useful, because you would just have to combine T1 and T2 in a single object, and use the first interface instead.

    More interesting, Guava also has a whole lot of methods using these two interfaces, like Iterables.any(Iterable, Predicate), Iterables.transform(Iterable, Function), etc.