Minborg

Minborg
Minborg

Thursday, December 1, 2016

Day 1, Java Holiday Calendar 2016, Functional Interfaces

1. Use the @FunctionalInterface Annotation



Today's tips is to annotate a functional interface with the @FunctionalInterface annotation thereby signaling that API users may use lambdas to implement the interface. It also ensures that the interface remains usable for lambdas over time by preventing abstract methods from accidentally being added to the API later on.

Do This:

@FunctionalInterface
public interface CircleSegmentConstructor {

    CircleSegment apply(Point cntr, Point p, double ang);

    // abstract methods cannot be added

}


Don't Do This:

public interface CircleSegmentConstructor {

    CircleSegment apply(Point cntr, Point p, double ang);

    // abstract methods may be accidentally added later

}


Read more in the original article at https://dzone.com/articles/the-java-8-api-design-principles

Follow the Java Holiday Calendar 2016 with small tips and tricks all the way through the winter holiday season.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.