Java Reference
In-Depth Information
Vector is subclassed by the concrete Stack class, which represents a LIFO data
structure. Stack providesan E push(E item) methodforpushinganobjectonto
thestack,an E pop() methodforpoppinganitemoffthetopofthestack,andafew
othermethods,suchas boolean empty() fordeterminingwhetherornotthestack
is empty.
Stack isagoodexampleofbadAPIdesign.Byinheritingfrom Vector ,itispos-
sible to call Vector 's void add(int index, E element) method to add
anelementanywhereyouwish,andviolatea Stack instance'sintegrity.Inhindsight,
Stack should have used composition in its design: use a Vector instance to store a
Stack instance's elements.
Dictionary is an abstract superclass for subclasses that map keys to values.
The concrete Hashtable class is Dictionary 's only subclass. As with Vector ,
HashTable instances are synchronized, HashTable has been generified, and
HashTable has been retrofitted to support the Collections Framework.
Hashtable is subclassed by Properties , a concrete class representing a per-
sistent set of properties ( String -based key/value pairs that identify application set-
tings). Properties provides Object setProperty(String key, String
value) for storing a property, and public String getProperty(String
key) for returning a property's value.
Note Application'susepropertiesforvariouspurposes.Forexample,ifyourapplic-
ationhasagraphicaluserinterface,youmightpersistitsmainwindow'sscreenlocation
andsizetoafileviaa Properties objectsothattheapplicationcanrestorethewin-
dow's location and size when it next runs.
Properties is another good example of bad API design. By inheriting from
Hashtable , you can call Hashtable 's V put(K key, V value) method
to store an entry with a non- String key and/or a non- String value. In hindsight,
Properties should have leveraged composition: store a Properties instance's
elements in a Hashtable instance.
Note Chapter2 discusseswrapperclasses,whichishow Stack and Properties
should have been implemented.
Finally, BitSet isaconcreteclassthatdescribesavariable-lengthsetofbits.This
class's ability to represent bitsets of arbitrary length contrasts with the previously de-
scribedinteger-based,fixed-lengthbitsetthatislimitedtoamaximumnumberofmem-
bers: 32 members for an int -based bitset, or 64 members for a long -based bitset.
Search WWH ::




Custom Search