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.

Next we will define a message protocol called “operations”. It is within this protocol that we will<br />

define all the methods which will provide the functionality of the Stack. These methods will be push:,<br />

pop <strong><strong>an</strong>d</strong> top.<br />

The push: method is used to push something onto the top of the stack. It takes one argument, (<strong>an</strong><br />

object) <strong><strong>an</strong>d</strong> adds it to the top of the stack. To do this it uses the addLast: method inherited from<br />

OrderedCollection.<br />

push: new<strong>Object</strong><br />

self addLast: new<strong>Object</strong><br />

Next we will define the pop method. This method removes the top element from the stack <strong><strong>an</strong>d</strong><br />

returns it as the result of evaluating pop. Again it uses methods inherited from<br />

OrderedCollection to do this.<br />

pop<br />

self isEmpty<br />

ifTrue: [^nil]<br />

ifFalse: [^self removeLast].<br />

Finally, we will define the method top. This method is like pop, except that it does not remove the<br />

top most element from the stack. To do this we use the message<br />

last inherited from<br />

OrderedCollection.<br />

top<br />

self isEmpty<br />

ifTrue: [^nil]<br />

ifFalse: [^self last].<br />

This class is <strong>an</strong> excellent example of w hy <strong>Smalltalk</strong> is so powerful. We have defined a class which<br />

provides the functionality of a Stack (no matter what the contents of that stack will be) in just a few<br />

lines of code. Consider the amount of work required to achieve the same result in a l<strong>an</strong>guage such as<br />

Pascal or C (without purchasing additional libraries).<br />

10.8.2 The Queue class<br />

This class definition is very similar to the Stack example, it is therefore presented here with little<br />

additional comment.<br />

OrderedCollection variableSubclass: #Queue<br />

inst<strong>an</strong>ceVariableNames: ''<br />

classVariableNames: ''<br />

poolDictionaries: ''<br />

category: 'Additional-Collections'<br />

Again a class comment should be defined. This is left <strong>an</strong> <strong>an</strong> excersie for the reader. The methods<br />

should again be placed in a protocol called “operations”. Each of the methods are listed below.<br />

add: new<strong>Object</strong><br />

self addLast: new<strong>Object</strong>.<br />

delete<br />

self isEmpty<br />

ifTrue: [^nil]<br />

ifFalse: [self removeFirst].<br />

next<br />

self isEmpty<br />

ifTrue: [^nil]<br />

ifFalse: [^self first].<br />

The state of the class definition at this point is illustrated in Figure 10.5.<br />

93

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

Saved successfully!

Ooh no, something went wrong!