I have a method that's about ten lines of code. I want to create more methods that do exactly the same thing, except for a small calculation that's going to change one line of code. This is a perfect application for passing in a function pointer to replace that one line, but Java doesn't have function pointers. What's my best alternative?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Method references using the
::
operatorYou can use method references in method arguments where the method accepts a functional interface. A functional interface is any interface that contains only one abstract method. (A functional interface may contain one or more default methods or static methods.)
IntBinaryOperator
is a functional interface. Its abstract method,applyAsInt
, accepts twoint
s as its parameters and returns anint
.Math.max
also accepts twoint
s and returns anint
. In this example,A.method(Math::max);
makesparameter.applyAsInt
send its two input values toMath.max
and return the result of thatMath.max
.In general, you can use:
instead of:
which is short for:
For more information, see :: (double colon) operator in Java 8 and Java Language Specification §15.13.
New Java 8 Functional Interfaces and Method References using the
::
operator.Java 8 is able to maintain method references ( MyClass::new ) with "@ Functional Interface" pointers. There are no need for same method name, only same method signature required.
Example:
So, what we have here?
YOU SHOULD USE FUNCTIONAL INTERFACES FOR LISTENERS ONLY AND ONLY FOR THAT!
Because all other such function pointers are really bad for code readability and for ability to understand. However, direct method references sometimes come handy, with foreach for example.
There are several predefined Functional Interfaces:
For earlier Java versions you should try Guava Libraries, which has similar functionality, and syntax, as Adrian Petrescu has mentioned above.
For additional research look at Java 8 Cheatsheet
and thanks to The Guy with The Hat for the Java Language Specification §15.13 link.
Prior to Java 8, nearest substitute for function-pointer-like functionality was an anonymous class. For example:
But now in Java 8 we have a very neat alternative known as lambda expression, which can be used as:
where isBiggerThan is a method in
CustomClass
. We can also use method references here:Anonymous inner class
Say you want to have a function passed in with a
String
param that returns anint
.First you have to define an interface with the function as its only member, if you can't reuse an existing one.
A method that takes the pointer would just accept
StringFunction
instance like so:And would be called like so:
EDIT: In Java 8, you could call it with a lambda expression:
When there is a predefined number of different calculations you can do in that one line, using an enum is a quick, yet clear way to implement a strategy pattern.
Obviously, the strategy method declaration, as well as exactly one instance of each implementation are all defined in a single class/file.
You may also be interested to hear about work going on for Java 7 involving closures:
What’s the current state of closures in Java?
http://gafter.blogspot.com/2006/08/closures-for-java.html
http://tech.puredanger.com/java7/#closures