Static method reference pattern¶
The Static method reference pattern (often denoted as ClassName::staticMethodName) is a Java 8 feature used to simplify the implementation of functional interfaces. It acts as a shorthand for a lambda expression that solely calls a static method, allowing developers to pass a specific static method as an argument directly^[600-developer__java__java8__java8-lambda01.md].
Application in Template Design¶
This pattern is frequently utilized when extracting common logic into template methods. By defining a Functional Interface that accepts a function as a parameter (such as a BiFunctionUnchecked), a generic template can handle the boilerplate code—such as resource initialization or transaction management—while delegating the specific business logic to the static method provided via the reference^[600-developer__java__java8__java8-lambda01.md].
Example¶
In the context of a database operation object (BO), the pattern replaces verbose anonymous inner classes or lambda expressions with a direct reference to the Data Access Object (DAO) method^[600-developer__java__java8__java8-lambda01.md].
java
// Invoking the template method
// BeanDao::currentSql is the static method reference
private static List<OhSystemConfig> simpleBoMethod(Map<String, Object> map) throws Exception {
return DefaultBO.read(map, BeanDao::currentSql);
}^[600-developer__java__java8__java8-lambda01.md]
Sources¶
^[600-developer__java__java8__java8-lambda01.md]
Related¶
- Functional Interface
- [[Lambda Expression]]
- [[Java 8]]