Posts

Showing posts from June, 2012

Applying the Strategy Pattern in Java

Image
Working with strategy on Arrays.sort & java.util.Comparator All these examples and more can be found on github In Java, you cannot pass function references through a method. With this being true, how do we then apply a strategy to a method? What is suggested, is that we define an interface with the function we want to pass. This way we can on the fly, define and pass an algorithm to a method for process. In this demo, we will be working strictly with the strategy interface:   java.util.Comparator . This is a java strategy interface, readily available in the Java API as of 1.5. It supplies a compare algorithm, where its implementation is primarily for sorting large collections of objects.