Java Reference
In-Depth Information
tending your class. Do not casually make fields of your classes protec-
ted: Both interfaces are real contracts, and both should be designed
carefully.
3.11.1. Designing an Extensible Framework
Suppose you want to provide a benchmarking harness for comparing
varieties of sorting algorithms. Some things can be said of all sorting
algorithm benchmarks: They all have data on which they must operate;
that data must support an ordering mechanism; and the number of
comparisons and swaps they require to do their work is an important
factor in the benchmark.
You can write an abstract class that helps you with these features, but
you cannot write a general-purpose sort methodthe actual operations
of sorting are determined by each extended class. Here is a SortDouble
class that sorts arrays of double values, tracking the number of swaps,
comparisons, and tests required in a SortMetrics class we define later:
abstract class SortDouble {
private double[] values;
private final SortMetrics curMetrics = new
SortMetrics();
/** Invoked to do the full sort */
public final SortMetrics sort(double[] data) {
values = data;
curMetrics.init();
doSort();
return getMetrics();
}
public final SortMetrics getMetrics() {
return curMetrics.clone();
}
/** For extended classes to know the number of
 
Search WWH ::




Custom Search