Coding the sort
To implement the sorting, first, we will define the interfaces that the library should implement. Defining the interface before actually coding is a good practice. When there are many implementations, it is sometimes recommended to first create a simple one and start using it so that the interface may evolve during this development phase, and when the more complex implementations are due, then the interface is already fixed. In reality, nothing is fixed ever because there is no Archimedean point in programming.
Creating the interfaces
The interface in our case is very simple:
public interface Sort { void sort(Sortable collection); }
The interface should do only one thing, sort something that is sortable. As such, we define an interface and any class that implements this interface will be Sortable
:
public interface Sortable { }
Creating BubbleSort
Now, we can start creating the bubble sort that implements the Sort
interface:
... import java.util.Comparator; public class BubbleSort...