Working with strategy on Arrays.sort & java.util.Comparator
All these examples and more can be found on github
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
comparealgorithm, where its implementation is primarily for sorting large collections of objects.
Here is a look at the Comparator interface.
One primary strategy pattern in Java, uses the comparator for sorting arrays in some form or fashion. You can call
Arrays.sort(array, comparator)to organize an array of objects based on a certain compare rule. With this implementation, you pass in your array object to sort, and the strategy algorithm to determine how to compare objects when sorting.
Simple Example of Sort Strategy
Here I would like to show an example of using the strategy pattern. First we will define an interface of type animal, and an implementing class. This is the object we will use for adding to an array and sorting the array.
Finally in the main, we can build the animal list, supply our compare strategy, and print the ordered Array to console.
And the output:
Taking a closer look at the Comparator
In this example, we are using the Strategy Pattern for sorting an Array of animals by the animals Class type. The comparator algorithm follows a certain set of rules defined by the Java Team.
When comparing the objects, using compare, you want:
- Equal objects to return 0
- Different objects to either return a positive or negative integer depending on priority.
concerned about the final exact order of the objects, we just want to ensure that like Animals classes are grouped together.
So in our algorithm, we ensure that:
- Equal animal classes return a 0 compare;
- Animal classes with different hash codes return some value greater than or less than the compared animals classes hash code.
Using a concrete strategy
Sometimes, we have a strategy, that we need to apply often. In this case, it may be best to supply it as a concrete strategy. This is done, by defining and instantiating your strategy at the top of a class.
Here is an example of a singleton class holding static strategies for animal sorting.
To understand more about why I chose an Enum as a singleton, please take a look at my article on why Enum's make the best singletons.
Implementing Comparable's compare to strategy
With our concrete strategy, species sort, we can use the inherited compareTo method from interface Comparable. String class implements this method and ensures larger strings return negative, and smaller strings return positive values.
This is an example call to the concrete strategy:
In java we can consider that strategies are supplied by strategy objects. These objects should implement some strategy interface allowing implementation to depend on the needs of the client.
To see the final results, feel free to pull down the source code from github. This project will be updated with Java best practice concepts and examples. There is no UI, but you can run the main class
RunAnimalStrategyDemo.javato test the strategy demo.