0

Imagine the following Interface:

public interface ActivationFunction {
    double calculate(double value);
}

with two similar implementations:

Class based:

public class SignFunction implements ActivationFunction {
    @Override
    public double calculate(double value) {
        return value >= 0.0 ? 1.0 : -1.0;
    }
}
...
final SignFunction signFunction = new SignFunction();

Interface based:

public interface SignFunction extends ActivationFunction {
    @Override
    default double calculate(double value) {
        return value >= 0.0 ? 1.0 : -1.0;
    }
}
...
final SignFunction signFunction = new SignFunction() {
};

Which of them is preferable and why?

Appreciate for your opinion

Alex Saunin
  • 976
  • 1
  • 8
  • 15
  • 2
    Neither. The whole purpose of a `@FunctionalInterface` is to verify that it can be used as a lambda: `final SignFunction signFunction = (value) value >= 0.0 ? 1.0 : -1.0;` – Sean Patrick Floyd Dec 11 '17 at 19:22
  • @SeanPatrickFloyd There might be other implementations: StepFunction, SigmoidFunction & etc – Alex Saunin Dec 11 '17 at 19:24
  • 1
    @AlexSaunin So? – shmosel Dec 11 '17 at 19:25
  • if all these classes are simply functions that implement calculate why not make the SignFunction a static class that implements the ActivationFunction interface since it doesn't seem like creating the object is neccessary? https://stackoverflow.com/questions/7486012/static-classes-in-java – RAZ_Muh_Taz Dec 11 '17 at 19:30
  • @shmosel It's better to make these functions reusable as classes or interfaces. There can be some other default methods based on `calculate` one. For ex: `default double[] calculate(double[] values) {...}` – Alex Saunin Dec 11 '17 at 19:42
  • 1
    Still not getting what that has to do with using a lambda vs. an explicit subclass. – shmosel Dec 11 '17 at 19:44
  • @shmosel Actually that is my question about. If there're any advantages with an interface implementation or not!? – Alex Saunin Dec 11 '17 at 19:58
  • I don't know what you mean by "interface implementation". Neither of your examples demonstrate the proper way of implementing a functional interface. – shmosel Dec 11 '17 at 20:23
  • Totally removed any allusions to the `@FunctionalInterface` from the initial question. – Alex Saunin Dec 11 '17 at 20:35

1 Answers1

1

Both work just fine so to some extent its just a matter of personal preference and how it will be used in your application. In a similar situation I would pick one of the mechanisms and use it until it became clear that one method was more appropriate than the other.

If you want to try and make the "correct" decision now then the main advantage (in my opinion anyway) of the interface mechanism is that it allows the behaviour of classes to be composed from multiple interfaces. You could have a class that gets implementation of functions from multiple interfaces which you can't do through plain old class inheritance.

The other advantage is that it forces the functions to be stateless which makes debugging them and understanding them easier. It's also less likely that someone will introduce state into your functions needlessly.

The downside, as illustrated by another answer to this post, is that not all developers realise that you can have implementation in an interface so you might get some push back from other developers.

You will also need to instantiate an object somewhere to be able to call the method on the interface.

matt helliwell
  • 2,574
  • 16
  • 24