Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Smalltalk and Object Orientation: an Introduction - Free
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
• OrderedCollection. A collection where the ordering is given by the order in which<br />
elements were added.<br />
• Array. A collection which provides array like behaviour.<br />
• String. An indexed collection of Characters.<br />
• SortedCollection. A collection of objects, which are sorted accordi ng to <strong>an</strong> ordering<br />
defined in the inst<strong>an</strong>ce of SortedCollection.<br />
• Symbol. A subclass of String. All symbols are unique, while there c<strong>an</strong> be two different<br />
Strings containing the same characters.<br />
• Text. A class which underst<strong><strong>an</strong>d</strong>s a notion of a collection of fonts associated with a string.<br />
A relatively new class which incorporates some of the more useful features of the Array,<br />
OrderedCollection with SortedCollection classes is the List class.<br />
The basic operations that are performed by the collection data structures include adding <strong><strong>an</strong>d</strong><br />
removing elements, determining the size of the collection, querying the presence or absence of elements<br />
<strong><strong>an</strong>d</strong> iterating over the elements.<br />
10.3 Choosing a Collection class<br />
It c<strong>an</strong> sometimes be confusing for those new to <strong>Smalltalk</strong> to decide whi ch collection class to use. Some<br />
make the mistake of always using <strong>an</strong> array (because it is similar to the constructs that they are used to).<br />
However, this is failing to underst<strong><strong>an</strong>d</strong> the way one should work with collections (data structures) in<br />
<strong>Smalltalk</strong>. To this end the following decision tree may be of use.<br />
Collection<br />
no<br />
Are elements ordered?<br />
yes<br />
Accessible by key?<br />
SequenceableCollection<br />
yes<br />
no<br />
order determined<br />
Access test<br />
Duplicate allowed?<br />
externally<br />
internally<br />
= = = yes no Accessible by key?<br />
class of elements<br />
Dictionary IdentityDictionary Bag Set<br />
yes<br />
no<br />
Number<br />
<strong>an</strong>y<br />
ArrayedCollection class of elements Interval SortedCollection<br />
class of elements<br />
SmallInteger Character <strong>an</strong>y<br />
<strong>an</strong>y<br />
Link<br />
ByteArray String Text Array RunArray List OrderedCollection LinkedList<br />
Figure 10.2: Selecting which Collection class to use<br />
The most commonly used collection classes are, Array, SortedCollection,<br />
OrderedCollection, List, Dictionary <strong><strong>an</strong>d</strong> Set.<br />
10.4 The Collection class<br />
The collection class is the abstract superclass which acts as the root of all collection classes. It provides<br />
facilities for creating inst<strong>an</strong>ces of a collection class, adding <strong>an</strong> element to a collection, accessing<br />
elements in a collection, removing <strong>an</strong> element from a collection <strong><strong>an</strong>d</strong> indicating the size of the collection.<br />
It also provides facilities which allow <strong>an</strong> operation to be applied to all elements of the collect ion (in a<br />
similar fashion to the mapcar function in Lisp). It also provides a number of conversion routines which<br />
allow one collection to be converted into <strong>an</strong>other.<br />
With one or two exceptions (e.g. ByteArray) <strong>an</strong>y object c<strong>an</strong> be stored into <strong>an</strong>y collection. The<br />
only message that these operations must be able to respond to is = (although objects to be placed in a<br />
Sorted collection must also respond to