Saturday, August 23, 2014

EJ-21 Use Function Objects to Represent Strategies

Ref: Effective Java by Joshua Bloch

Strategy classes are often declered using anonymous classes and pessed to target operation. The following statement sorts an array of strings according to length:

But note that using an anonymous class in this way will create a new instance each time the call is executed. When it is expected for repeated use, it is generally implemented as a private static member class and exported in a public static final field whose type is the strategy interface

No comments: