Java Reference
In-Depth Information
more often than they are changed; yet another design might be optim-
ized for sets that change frequently. If there were a package of various
implementations for the Attributed interface, a class might choose to im-
plement 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 Attributed , so the class must implement all
the interface's methods. AttributedImpl implements the methods using
a HashMap , described in " HashMap " on page 590 . Later, this implement-
ation is used to implement the Attributed interface for a specific set of
objects to which you would want to add attributes. First, here is the At-
tributedImpl class:
import java.util.*;
class AttributedImpl implements Attributed, Iterable<Attr> {
protected Map<String, Attr> attrTable =
new HashMap<String, Attr>();
public void add(Attr newAttr) {
attrTable.put(newAttr.getName(), newAttr);
}
public Attr find(String name) {
return attrTable.get(name);
}
public Attr remove(String name) {
return attrTable.remove(name);
}
public Iterator<Attr> attrs() {
return attrTable.values().iterator();
}
public Iterator<Attr> iterator() {
 
Search WWH ::




Custom Search