THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

To create an Attributed type you could define a class that would form the superclass for all attributed
objects. But then programmers must decide whether to inherit from Attributed or from some other useful
class. Instead we make Attributed into an interface:


public interface Attributed {
void add(Attr newAttr);
Attr find(String attrName);
Attr remove(String attrName);
java.util.Iterator attrs();
}


This interface declares four methods: one for adding a new attribute to an Attributed object; one for
finding whether an attribute of a given name has been added to that object; one for removing an attribute from
an object; and one for accessing all of the attributes currently attached to the object.


When we add an attribute to an object, that object considers itself the owner of that Attr instance until it is
removed. If the attribute has its value changed or is shared among a number of objects, then the expectation is
that the programmer makes such changes and performs such sharing in a manner that makes sense to the
application. If the programmer is not to be trusted in this, then methods should specify that they make
defensive copies of parameters, and/or return values, such that no harmful changes can occur.


The attributes are accessed through an Iterator object returned from the attrs method. Iterator is a
generic interface defined in java.util for collection classes to use to provide access to their contents (see
"Iteration" on page 571). In effect, the Attributed interface defines a collection typea set of attributesso
we use the normal mechanism for accessing the contents of a collection, namely, the Iterator type. Using
Iterator has another benefit: It is easy to implement Attributed with a standard collection class (such
as HashMap) that uses Iterator, as you'll soon see.


Many classes that provide an Iterator declare that they implement the Iterable interface, which
defines the single method iterator to return an Iterator instance. Although an Attributed object
does provide an Iterator, it would be wrong to have Attributed extend Iterable or to rename
attrs as iterator, because that would restrict the ability of the class implementing Attributed to
control its own iteration behavior. For example, it would mean that an Attributed collection class would
not be able to provide an iterator for its elements rather than its attributes.


4.4.1. Implementing Interfaces


Interfaces describe contracts in a pure, abstract form, but an interface is interesting only if a class implements
it.


Some interfaces are purely abstractthey do not have any useful general implementation but must be
implemented afresh for each new class. Most interfaces, however, may have several useful implementations.
In the case of our Attributed interface, we can imagine several possible implementations that use various
strategies to store a set of attributes.


One strategy might be simple and fast when only a few attributes are in a set; another one might be optimized
for attribute sets that are queried more often than they are changed; yet another design might be optimized for
sets that change frequently. If there were a package of various implementations for the Attributed
interface, a class might choose to implement the Attributed interface through any one of them or through
its own implementation.


As an example, here is a simple implementation of Attributed that uses the utility
java.util.HashMap class. The class AttributedImpl declares that it implements the interface

Free download pdf