29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

holding other objects. Typically, collections are used as data structures in <strong>Smalltalk</strong> systems. They c<strong>an</strong><br />

also be used as temporary holding places for groups of calculations, results, operations etc.<br />

The Collection class assumes that a new subclass will provide at least the following:<br />

• add: This adds <strong>an</strong> object to the collection<br />

• remove:ifAbsent: This removes <strong>an</strong> object from the collection. If it is not present in the<br />

collection, the block provided as the parameter to ifAbsent: is evaluated.<br />

• do: This applies the associated block to each element in the collection<br />

• copyEmpty: (if the subclass possesses named inst<strong>an</strong>ce variables). This creates a copy of the<br />

receiving collection which contains no elements.<br />

The Collection class defines the common protocols to which all types of collection are expected to<br />

respond. For example, objects may be added to collections using:<br />

• aCollection add: <strong>an</strong><strong>Object</strong> Subclasses redefine this message such that it will add a<br />

single inst<strong>an</strong>ce of a n<strong>Object</strong> to the collection. In some classes this is dependent on the current<br />

contents of the collection (e.g. class Set).<br />

• aCollection addAll: oldCollection Add each element in oldCollection to<br />

aCollection. This method uses add: <strong><strong>an</strong>d</strong> therefore does not need to be redefined in a subclass.<br />

• size. The current size of the collection (i.e. the number of objects it contains) may be<br />

obtained using the size message: aCollection size.<br />

<strong>Object</strong>s may be removed using one of the following:<br />

• aCollection remove: old<strong>Object</strong>. Remove the first element which equals<br />

old<strong>Object</strong><br />

• aCollection remove: old<strong>Object</strong> ifAbsent: aBlock. Subclasses redefine this<br />

method. The intention is that the method will remove the first inst<strong>an</strong>ce of old<strong>Object</strong>. They<br />

will run aBlock if that object is not present in the collection.<br />

• removeAll: aCollection. This method removes all of the element contained in<br />

aCollection from the receiving collection.<br />

The Collection class also define s a r<strong>an</strong>ge of test methods which c<strong>an</strong> be used to identify the current<br />

state of a collection or determine if <strong>an</strong> object is a member of a collection. The test operations supported<br />

include:<br />

• aCollection includes: <strong>an</strong><strong>Object</strong>. This test s whether <strong>an</strong> element is currently a<br />

member of a collection. This method uses the = test.<br />

• aCollection isEmpty. This message returns true or false depending on whether the<br />

collection contains <strong>an</strong>y objects or not.<br />

• aCollection occurrencesOf: <strong>an</strong><strong>Object</strong>. This message returns the number of times<br />

<strong>an</strong><strong>Object</strong> is present in the collection. Again this uses the #= test.<br />

Additional protocols are implemented by subclasses to support operations suitable only for that<br />

class. For example, OrderedCollection supports first, atFirst:, after: <strong><strong>an</strong>d</strong><br />

add:before: while arrays underst<strong><strong>an</strong>d</strong> at: <strong><strong>an</strong>d</strong> at:put:.<br />

10.5 Bag<br />

Abstractly a Bag c<strong>an</strong> be considered to be <strong>an</strong>y collection of objects, which c<strong>an</strong> be of <strong>an</strong>y class; these<br />

objects are the elements of the Bag. It is a general place holder for collections of objects. There is no<br />

order assumed. It is the most general form of collection available in <strong>Smalltalk</strong>. (In m<strong>an</strong>y<br />

implementations each entry in the bag is actually <strong>an</strong> association which indicates the object <strong><strong>an</strong>d</strong> the<br />

number of times it has been placed in the bag. This is intended as a perform<strong>an</strong>ce enh<strong>an</strong>cement <strong><strong>an</strong>d</strong> is<br />

normally tr<strong>an</strong>sparent to the user.)<br />

89

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!