I'd say that anything that uses Java's ServiceLoader scheme is using the strategy pattern. Basically the algorithms (maybe a huge suite of them) are undecided until runtime. Service Loader itself becomes a sort of master strategy (maybe that's off topic here) but anything that uses the ServiceLoader
(e.g., CharsetDecoder
) is following the strategy pattern approach.
Edit to add in response to comment: My understanding of "strategy pattern" is that it is a parent object into which a specific algorithm or algorithms may be decided upon and injected at execution time. So ServiceLoader itself is not a strategy pattern, but facilitates many areas of the JDK and other applications via SPI that employ the strategy pattern.
But maybe I'm making it too difficult. Basically, Collections.sort(List, Comparator)
and any of the sorted collections with Comparator
constructor arguments (e.g., new TreeSet(Comparator)
are also examples. Why? Because, at execution time, any suitable comparator may be submitted to the sort()
or constructor in order to change behavior. Normally in the Strategy pattern, there would be a number of implementations that could be chosen at execution time -- think for example of a table of emails that may be sorted by increasing/decreasing date, subject, or from-address order. Each of these would have an associated Comparator
.