Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
package v2;

import java.util.Arrays;

public class SortMain {
    private static Box box1 = new Box();
    private static Parcel[] packs = { new Postpack(60, 12345), box1,
                                      new Postpack(50, 54321) };
    private static Sorter zipSorter = new ZipSorter();
    private static Sorter weightSorter = new WeightSorter();
    public static void main(String[] args) {
        box1.setWeight(55);
        box1.setZip(99999);
        System.out.println("Unsorted:         " + Arrays.asList(packs));
        System.out.println("Sorted by weight: " + weightSorter.sort(packs));
        System.out.println("Sorted by zip:    " + zipSorter.sort(packs));
    }
}

We need to compile the interfaces first, then we can compile the Groovy or Java files (excluding SortMain) in either order. Finally we compile SortMain as it is the class that knows about the concrete implementations. If we were using Spring dependency injection or our own factory methods we could have reduced or eliminated the need to treat SortMain as a special case, e.g. using Spring we could have the concrete classes listed in a an external beans.xml file and SortMain whether it was written in Java or Groovy could have been compiled along with all the other files written in its language.

...