Java Reference
In-Depth Information
•
public
The
public
part of the class is designed for use by the code
that tests how expensive the sorting algorithm is. An example of
testing code is in
TestSort.main
. This code provides the data to be
sorted and gets the results of the test. For the test code, the met-
rics are read-only. The public
sort
method we provide for the test
code ensures that the metrics are initialized before they are used.
Making the actual
doSort
method
protected
forces the test code to
invoke it indirectly by the public
sort
method; thus, we guarantee
that the metrics are always initialized and so avoid another pos-
sible error.
To the test code, the only available functionality of the class is to
drive a test of a particular sorting algorithm and provide the res-
ults. We used methods and access protection to hide the rest of
the class, which should not be exposed to the testing code.
•
protected
The
protected
part of the class is designed for use by
the sorting code to produce a properly metered sort. The
protec-
ted
contract lets the sorting algorithm examine and modify the
data to produce a sorted list by whatever means the sort desires.
It also gives the sorting algorithm a context in which it will be
properly driven so that it can be measured. This context is the
doSort
method.
The extended class is not considered trustworthy, and that is why
it can access the data only indirectly, through methods that have
access to the data. For example, to hide a comparison by avoid-
ing
compare
, the sort would have to use
probe
to find out what is in
the array. Because calls to
probe
are also metered, this would, in
the end, hide nothing.
In addition,
getMetrics
returns a clone of the actual metrics, so a
sorting implementation cannot modify the values.
•
private
The class keeps private to itself data that should be hid-
den from the outsidenamely, the data being sorted and the met-
rics. Outside code cannot access these fields, directly or indirec-
tly.