Java Reference
In-Depth Information
Exercise 10.58 Write a class that can make comparisons between the efficiency of the
common methods from the List interface in the ArrayList and LinkedList classes
such as add , get , and remove . Use the polymorphic-variable technique described above to
write the class so that it only knows it is performing its tests on objects of the interface type
List rather than on the concrete types ArrayList and LinkedList . Use large lists of
objects for the tests, to make the results significant. You can use the currentTimeMillis
method of the System class for getting hold of the start and finish time of your test methods.
Exercise 10.59 Read the API description for the sort methods of the Collections
class in the java.util package. Which interfaces are mentioned in the descriptions?
Exercise 10.60 Challenge exercise Investigate the Comparable interface. This is a
parameterized interface. Define a simple class that implements Comparable . Create a collec-
tion containing objects of this class and sort the collection. Hint : The LogEntry class of the
weblog-analyzer project in Chapter 4 implements this interface.
10.6.5
Library support through abstract classes and interfaces
In Chapter 5, we pointed out the importance of paying attention to the names of the collec-
tion classes: ArrayList , LinkedList , HashSet , TreeSet , etc. Now that we have been
introduced to abstract classes and interfaces, we can see why these particular names have been
chosen. The java.util package defines several important collection abstractions in the form
of interfaces, such as List , Map and, Set . The concrete class names have been chosen to com-
municate information about both what kind of interface they conform to and some of the under-
lying implementation detail. This information is very useful when it comes to making informed
decisions about the right concrete class to use in a particular setting. However, by using the
highest-level abstract type (be it abstract class or interface) for our variables, wherever possible,
our code will remain flexible in light of future library changes—such as the addition of a new
Map or Set implementation, for instance.
In Chapter 11, where we introduce the Java GUI libraries, we will be making enormous use of
abstract classes and interfaces as we see how to create quite sophisticated functionality with
very little additional code.
10.7
A further example of interfaces
In the previous section, we have discussed how interfaces can be used to separate the specifica-
tion of a component from its implementation so that different implementations can be “plugged
in,” thus making it easy to replace components of a system. This is often done to separate parts
of a system that are logically only loosely coupled.
We have seen an example of this (without discussing it explicitly) at the end of Section 10.3.
There, we investigated the foxes-and-rabbits-graph project, which added another view of the
populations in the form of a line graph. Looking at the class diagram for this project, we can see
that the addition also makes use of a Java interface (Fig 10.6).
 
 
Search WWH ::




Custom Search