0

I have an interface with only a single method that could be replaced by java.util.function.Function from the JDK:

interface DataModel {
  boolean apply(Map<String, Integer> input);
}

The replacement would be

Function<Map<String, Integer>, Boolean>

However, I wonder whether I should keep this interface since at least one implementation (other implementations might follow) contains mutable state and is not thread safe. A factory creates a new instance for each call.

So while technically possible, the functional interface might semantically not the best solution.

The API documentation doesn't say something about mutability, but maybe it would violate the principle of least astonishment or some other kind of contract. Is there any such contract?

Would you go with Function or with the custom DataModel interface and why?

2
  • 1
    What problem are you solving? That's what dictates whether I use Function or not. If you are doing functional programming where you have to provide a function, I would lean toward the Function interface since that speaks toward the functional mindset better. If I'm in a more DDD environment I would probably favor DataModel. Context is everything. Commented Dec 19, 2017 at 14:46
  • It is not a functional programming context. The interface is only accidental functional (what could be the answer to my question ...).
    – deamon
    Commented Dec 19, 2017 at 14:49

1 Answer 1

1

I would got for DataModel interface, to avoid future missunderstandings and problems.
I not a Imperative/OO or Functional purist, but I think keeping things clear is a good thing.
@FunctionalInterfaces has it's own purposes, I think custom interfaces always will fit better for "concrete implementations"; you own them, so you can extend and document them whenever you want or need.
Finally, try to avoid this scenario: "a new change will force us to migrate Funtions implementations to BiFunctions, or anything else"; imagine the refactorings...
Something you can do is extend Function, to have your own copy of the interface and reuse default methods if needed:

public interface DataModel<T, R> extends Function<T, R> {}
1
  • Tanks, I go with my plain DataModel interface since it is only accidentally a functional interface.
    – deamon
    Commented Dec 20, 2017 at 8:44

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.