Java Reference
In-Depth Information
ation is that the programmer makes such changes and performs such
sharing in a manner that makes sense to the application. If the pro-
grammer 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 "
Itera-
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
Iterat-
or
, it would be wrong to have
Attributed
extend
Iterable
or to rename
attrs
as
iterator
, because that would restrict the ability of the class im-
plementing
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 im-
plementations 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