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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

If you are confused by this description of a bag, think of it as a shopping bag. At a supermarket, you<br />

pick objects up from the shelves <strong><strong>an</strong>d</strong> place them in your shopping bag. For example, you pick up a pint<br />

of milk, a box of corn flakes, a packet of biscuits, three bags of potato crisps, <strong><strong>an</strong>d</strong> a few b<strong>an</strong><strong>an</strong>as (see<br />

Figure 10.3).<br />

Corn<br />

Flakes<br />

Salt n'<br />

Vinegar<br />

Milk<br />

Biscuits<br />

Figure 10.3: A shopping bag<br />

Each of the objects in the bag is a different type of thing, with different characteristics etc. There is<br />

no particular order to them, they will have moved about in the bag w hile you were shopping <strong><strong>an</strong>d</strong> while<br />

you brought them home. When you reach into the bag at home to remove the objects, the order in which<br />

they come out will not be predictable. If you think of a bag collection in these terms then you will not be<br />

far off the mark.<br />

As with <strong>an</strong>y other class we create <strong>an</strong> inst<strong>an</strong>ce of Bag by sending the message new to the class Bag.<br />

For example:<br />

| temp |<br />

temp := Bag new.<br />

The bag object responds to the add: message to add objects as well as the add:withOccurrences:<br />

messages which c<strong>an</strong> be used to add <strong>an</strong> object n times. For example:<br />

temp add: 'John'.<br />

temp add: 'Hello' withOccurrences: 2.<br />

At this point the bag object will contain three objects, the string ‘John’ <strong><strong>an</strong>d</strong> two copies of the string<br />

‘Hello’. We c<strong>an</strong> examine the contents of the bag using the inspect message, for example:<br />

temp inspect.<br />

You will notice that the bag object does not record the objects in the order in which they were<br />

added. Instead, the order is not specified (it is actually determined for efficiency). You will also notice<br />

that there are inst<strong>an</strong>ce variables numbered 1 - 7. This is because collections are a special type of object<br />

whose inst<strong>an</strong>ce variables are defined dynamically as <strong><strong>an</strong>d</strong> when required. They are termed “variable”<br />

classes.<br />

It is also possible to remove objects from a set using either the remove: ifAbsent: message or<br />

the removeAllOccurrencesOf: ifAbsent: message. The first is used to remove a single<br />

reference to <strong>an</strong> object, while the second is used to remove all reference s to <strong>an</strong> object. In both cases a<br />

block must be supplied which is executed if the object is not a member of the bag. For example:<br />

temp remove: 'Paul' ifAbsent: [].<br />

temp remove: 'Paul' ifAbsent: [Tr<strong>an</strong>script show: 'No Record of Paul'].<br />

In the first example, if the string ‘Paul’ is not in the bag then the empty block will be executed, which<br />

will result in nothing happening. In the second example, a message will be printed to the Tr<strong>an</strong>script.<br />

10.6 Set<br />

The class Set is basically the same as the class Bag, with the exception that it does not allow<br />

duplicates. That is, it is only possible to hold a single reference to <strong>an</strong> object in a set. For example, try the<br />

following out in a Workspace:<br />

90

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

Saved successfully!

Ooh no, something went wrong!