569 lines
32 KiB
HTML
569 lines
32 KiB
HTML
<!--startcut ==============================================-->
|
|
<!-- *** BEGIN HTML header *** -->
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<HTML><HEAD>
|
|
<title>Making Smalltalk: Bags o' Fun LG #61</title>
|
|
</HEAD>
|
|
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
|
|
ALINK="#FF0000">
|
|
<!-- *** END HTML header *** -->
|
|
|
|
<CENTER>
|
|
<A HREF="http://www.linuxgazette.com/">
|
|
<H1><IMG ALT="LINUX GAZETTE" SRC="../gx/lglogo.jpg"
|
|
WIDTH="600" HEIGHT="124" border="0"></H1></A>
|
|
|
|
<!-- *** BEGIN navbar *** -->
|
|
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="padala.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue61/steffler.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="ward.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
|
|
<!-- *** END navbar *** -->
|
|
<P>
|
|
</CENTER>
|
|
|
|
<!--endcut ============================================================-->
|
|
|
|
<H4 ALIGN="center">
|
|
"Linux Gazette...<I>making Linux just a little more fun!</I>"
|
|
</H4>
|
|
|
|
<P> <HR> <P>
|
|
<!--===================================================================-->
|
|
|
|
<!-- END header -->
|
|
|
|
|
|
|
|
|
|
<img SRC="misc/steffler/penguinInBalloon.gif" height=230 width=200 align=right>
|
|
<center>
|
|
<h1>
|
|
<img SRC="misc/steffler/makingSmalltalk.png" ></h1></center>
|
|
|
|
<center>
|
|
<h3>Bags o' Fun</h3>
|
|
<H4>By <a href="mailto:jagwar@magma.ca">Jason Steffler</a></H4></center>
|
|
|
|
<H2><a NAME="abstract"></a>Abstract</h2>
|
|
For those who haven't read the previous articles, be
|
|
sure to read <font size=+1><a href="#statementOfPurpose">the statement
|
|
of purpose</a></font> first. This month, we're going to discuss inheritence,
|
|
polymorphism, and abstract classes as well as introducing some collection
|
|
objects. For those looking to read the whole series locally or information
|
|
about upcoming articles, you can
|
|
<font size=+1><a href="http://www.magma.ca/~jagwar/makingSmalltalkForwardingPage.html">check
|
|
the MST page</a></font>. For those looking for further information
|
|
on learning Squeak, here are <a href="http://minnow.cc.gatech.edu/squeak/377">some
|
|
good resources</a>.
|
|
<h2>
|
|
<a NAME="quoteOfTheDay"></a>Quote of the day</h2>
|
|
"Why Smalltalk? Smalltalk uses a simplified version of English. Nouns and
|
|
verbs. Object are the nouns. Verbs are messages sent from one object to
|
|
the next. Easy as 1, 2, 3. It is no longer necessary to write cryptic programs.
|
|
This means that almost anyone can learn to write Smalltalk programs."
|
|
<br> - Peter William Lount
|
|
<h2>
|
|
<a NAME="inheritence"></a>Inheritence</h2>
|
|
Last month, we were using <b>aPerson</b> object for
|
|
illustrative purposes. This month we'll only build on this example
|
|
briefly, as it quickly gets too contrived for my liking. Also, this
|
|
is a good opportunity to introduce the collection objects of Smalltalk.
|
|
We'll return to <b>aPerson</b> next month though, as the emphasis will
|
|
be on the building aspect, and not on the object itself.
|
|
<br> The notion of inheritence in OO is very similar
|
|
to the normal use of this term. Just like a person can inherit certain
|
|
characteristics from their parents, so too can objects inherit certain
|
|
characteristics from their parents. Objects inherit the methods and
|
|
instance variables of their parents, among other things.
|
|
<br> To get this discussion going, lets introduce the
|
|
object <b>anOrderedCollection, </b>whose class is <b>OrderedCollection</b>.
|
|
This is a very common object to use in Smalltalk. An <b>OrderedCollection</b>
|
|
is just what it sounds like: it's a collection of objects where order
|
|
is important. To better position the topics at hand, it'd useful
|
|
to have an idea of how you can use this type of collection:
|
|
<p><i>[ex1]</i>
|
|
<br> "Inspect this code. Here we're creating a
|
|
new
|
|
<b>OrderedCollection</b>, and asking it to add 1, then add 2, then
|
|
add 3, then finally return yourself"
|
|
<br><b> (OrderedCollection new) add: 1; add: 2;
|
|
add: 3; yourself.</b>
|
|
<p><i>[ex2]</i>
|
|
<br> "This is normally not written on one line, but is
|
|
written like this:"
|
|
<br> <b>(OrderedCollection new)</b>
|
|
<br><b> add: 1;</b>
|
|
<br><b> add: 2;</b>
|
|
<br><b> add: 3;</b>
|
|
<br><b> yourself.</b>
|
|
<p> Here, we're asking the class (remember, this is a
|
|
blueprint for creating objects) <b>OrderedCollection</b> to create a new
|
|
collection. Then we're asking the new collection to add 1 to itself.
|
|
Then we're asking the same new collection to add 2 to itself, then 3, and
|
|
then finally we're asking the collection to return itself. You normally
|
|
don't have to send that last message to an object, as the default return
|
|
is the object itself (we call this the <a href="#articleGlossary">receiver</a>
|
|
of the the messages), but the message <b>add:</b> returns the parameter
|
|
you're passing, so in this case, if we want to see the <b>OrderedCollection</b>
|
|
that we're creating, we need to ask it to return itself as the last message
|
|
send. This may be a little confusing; I showed the above snippet
|
|
as it explicitly creates a new object. You could get the same results
|
|
by inspecting the below two snippets, that don't explictly create a new
|
|
object - this is done implicitly from the message sent:
|
|
<p><i>[ex3]</i>
|
|
<br> "Here, we're asking the <b>OrderedCollection</b>
|
|
class to give us a new <b>OrderedCollection</b> object with the values
|
|
1, 2, and 3 in it."
|
|
<br><b> OrderedCollection with: 1 with: 2 with: 3</b>
|
|
<p><i>[ex4]</i>
|
|
<br> "Here, we're asking the <b>OrderedCollection</b>
|
|
class to give us a new <b>OrderedCollection</b> object with all of the
|
|
values 1, 2 and 3<sup><a href="#footnotes">1</a></sup>"
|
|
<br> <b>OrderedCollection withAll: #(1 2 3)</b>
|
|
<p><i>[ex5]</i>
|
|
<br> Now, if you <b>print it</b> to the above code, you'll
|
|
see an ASCII representation of the object: <b>OrderedCollection (1
|
|
2 3 )</b>. When you inspect the above code, and click on <b>self</b>
|
|
you will see:
|
|
<br><img SRC="misc/steffler/orderedCollection1.png" height=203 width=273>
|
|
<p><i>[ex6]</i>
|
|
<br> There are several ways that we could make an ordered
|
|
collection with a fourth <b>Integer</b> in it, here's a neat way.
|
|
Say that you have this <b>OrderedCollection</b> with only the first 3 integers
|
|
in it, and realize 'wups, I actually wanted 4 integers in it.' You
|
|
don't have to go back to the code you typed in above and redo it, you can
|
|
just ask the object you're inspecting to add a fourth integer to itself:
|
|
<br><img SRC="misc/steffler/orderedCollection2.png" height=201 width=272>
|
|
<p><i>[ex7]</i>
|
|
<br> Highlight the code entered in the bottom pane, and
|
|
<b>do
|
|
it. </b>Here, you're asking the object itself (<b>self</b>) to add
|
|
4 to itself. If you have <b>self</b> highlighted, you'll notice that
|
|
it is updated (if you don't have <b>self</b> highlighted, then click on
|
|
it to see the updates. You'll see:
|
|
<br><img SRC="misc/steffler/orderedCollection3.png" height=199 width=249>
|
|
<p> This is an illustration of being able to view and
|
|
manipulate objects in real time, which is <b>Immensely Powerful</b>.
|
|
If you're coding along and something isn't quite working right, you can
|
|
stop execution, grab the troublesome object and see exactly what is going
|
|
on. If you want to simulate certain conditions, <u>you can just change
|
|
the object directly</u>. For example, say you realized that you shouldn't
|
|
have the integer 4, but rather the string 'four', you can click on the
|
|
fourth element, delete 4, and tye in <b>'four'</b>, then <b>middle click>accept.
|
|
</b>The
|
|
fourth element in this collection is now the string <b>'four'.
|
|
</b>Try
|
|
clicking on the 3rd element, then back to the fourth element to confirm
|
|
this, you'll see:
|
|
<br><img SRC="misc/steffler/orderedCollection4.png" height=199 width=249>
|
|
<p> ...and remember, we did all this without the hassle
|
|
of compiling, linking, and running the compiled program! Ok, now
|
|
that we have an idea about how to create a collection, we're going to do
|
|
something with this collection, lets add up the integers in the collection.
|
|
To do this, you can <b>do it</b> the following snippet:
|
|
<p><i>[ex8]</i>
|
|
<br><b> | anOrderedCollection aSum |</b>
|
|
<br><b> aSum := 0.</b>
|
|
<br><b> anOrderedCollection := OrderedCollection withAll:
|
|
#(1 2 3).</b>
|
|
<br><b> anOrderedCollection do: [:anElement | aSum :=
|
|
aSum + anElement].</b>
|
|
<br><b> aSum inspect.</b>
|
|
<p> Here, the lines of code mean:
|
|
<br> 1) declare temporary variables
|
|
<br> 2) initialize the sum
|
|
<br> 3) create a new ordered collection, assign it to
|
|
one of the temporary variables
|
|
<br> 4) ask the ordered collection to do something for
|
|
each element. For each element, we're asking the sum to add the element
|
|
to itself.
|
|
<br> 5) here, we're asking the sum to open an inspector
|
|
on itself (yeah, you can do this programatically - cool eh?)
|
|
<p> For the folks with programming experience, you'll
|
|
note that we didn't have to worry about bounds checking, or the size of
|
|
the collection, or declaring temporary variables to index the collection
|
|
- this is all handled by the collection. Very nice and it helps to
|
|
reduce errors. We very naturally just asked the ordered collection
|
|
to <b>do</b> something with each element.
|
|
<br> Back to inheritence now, as the name suggests, <b>OrderedCollection</b>
|
|
is a type of <b>Collection</b>, and inherits methods and instance variables
|
|
from <b>Collection. </b>To be more precise, it inherits from a class
|
|
called <b>SequenceableCollection</b>, which in turn inherits from <b>Collection.</b>
|
|
Now, I could use UML, or any number of other industry software modeling
|
|
diagrams here, but I want to save time so I'm going to use a textual shorthand
|
|
for outlining class relationships - I'll denote inheritence by tabbing,
|
|
so indicating the above inheritence looks like this:
|
|
<br><b> Collection</b>
|
|
<br><b> SequenceableCollection</b>
|
|
<br><b>
|
|
OrderedCollection</b>
|
|
<p> You can think of this as <b>OrderedCollection </b>is
|
|
a type of <b>SequenceableCollection</b>, which is a type of <b>Collection.</b>
|
|
For example, a creation method we used - <b>withAll:</b> is inherited from
|
|
<b>Collection</b>,
|
|
I'll show this <a href="#articleGlossary">class method</a> by:
|
|
<br><b> Collection</b>
|
|
<br><b> >>withAll:</b>
|
|
<br><b> SequenceableCollection</b>
|
|
<br><b>
|
|
OrderedCollection</b>
|
|
<p> Both <b>Collection</b>, and <b>SequenceableCollection</b>,
|
|
are what we call <a href="#articleGlossary">abstract classes</a> - classes
|
|
that would never instantiate an object themselves, but serve as good logical
|
|
building points. Here, it doesn't matter if we have an <b>OrderedCollection,
|
|
or
|
|
</b>a <b>SortedCollection</b>, or a <b>Bag </b>(an unordered collection),
|
|
or whatever - we'd want all of them to know how to respond to <b>withAll:</b>.
|
|
<u>Here's
|
|
the sweet thing:</u> we implement the method that all these classes
|
|
should respond to <i>in one spot<b>,</b> and reuse it. </i>So, if you need
|
|
to change withAll: for these classes, <i>then there's only one spot to
|
|
go to</i>.
|
|
<br> If you need to have an exception to the rule, say
|
|
you have have a <b>Heap</b><sup><a href="#footnotes">2</a></sup> class
|
|
that needs to implement the <b>withAll: </b>method differently, then you
|
|
can do what is called <a href="#articleGlossary">overriding</a> the method
|
|
in <b>Heap</b>. Adding <b>Heap</b> to our outline, and indicating
|
|
abstract classes in italics gives us:
|
|
<br><b><i> Collection</i></b>
|
|
<br><b> >>withAll:</b>
|
|
<br><b><i> SequenceableCollection</i></b>
|
|
<br><b>
|
|
OrderedCollection</b>
|
|
<br><b>
|
|
Heap</b>
|
|
<br><b>
|
|
>>withAll:</b>
|
|
<p> Note: when we send the <b>withAll:</b> message
|
|
to <b>Heap</b> or to <b>OrderedCollection, </b><i>these two classes have
|
|
different implementation of the same message</i> - this is known as <a href="#articleGlossary">polymorphism</a><i>.
|
|
</i>This
|
|
is another one of those esoteric terms that really means something pretty
|
|
simple.
|
|
<br> The corollary of polymorphism is a very powerful
|
|
one though, it allows you to <i>get out of a decision making</i> frame
|
|
of mind, and <i>get into a commanding frame</i> of mind.<i> </i>This allows
|
|
us to get away from a common procedural programming trait - having lot
|
|
of code that is checking stuff and conditionally doing stuff (if it's an
|
|
<b>OrderedCollection</b>,
|
|
do this, if it's a <b>Heap</b>, do this, if it's a <b>Bag</b>, do this,
|
|
etc), and lets us just do stuff. It doesn't matter what type of collection
|
|
it is, when we ask it to do <b>withAll:</b>, it will do the right thing!
|
|
<br> Finally, if we also add the above mentioned <b>SortedCollection
|
|
</b>and<b>
|
|
Bag </b>(<groan> here's where this month's title pun comes from ;-),
|
|
we get:
|
|
<br><b><i> Collection</i></b>
|
|
<br><b> >>withAll:</b>
|
|
<br><b> Bag</b>
|
|
<br><b><i> SequenceableCollection</i></b>
|
|
<br><b>
|
|
OrderedCollection</b>
|
|
<br><b>
|
|
SortedCollection</b>
|
|
<br><b>
|
|
Heap</b>
|
|
<br><b>
|
|
>>withAll:</b>
|
|
<p><b> </b>It's easy to see how there are lots of opportunities
|
|
for reuse here, it's generally a good thing when you can code something
|
|
in one spot, and have many objects reuse that one implementation.
|
|
That way, when you have to make an udpate, you only update that one spot
|
|
and don't have to worry with tracking down many different spots and keeping
|
|
the update in synch.
|
|
<br> Now we're going to start getting to the question
|
|
of how we know what objects are where and how to use them. As with
|
|
other topics in this series, I'm introducing this one a bit at a time as
|
|
well. A common problem for Smalltalk beginners is that they're overwhelmed
|
|
with the rich class library as there are thousands of objects you can use.
|
|
To help reduce this problem, I've extended one of the Smalltalk browsers
|
|
and made a <b>ScopedBrowser</b>. This is a good example of the reflectiveness
|
|
we mentioned earlier - I was able to extend or alter the behavior of the
|
|
IDE to suit my needs. This <b>ScopedBrowser</b> will only show you
|
|
the classes we need to concentrate on for this article. My intent
|
|
is to add to the scope that is being browsed over time as more objects
|
|
are introduced. For this time, I've included all the above mentioned
|
|
collections objects as well as a couple more collections objects for those
|
|
interested (a total of 9 classes). To open this browser, you first
|
|
need to file in the <a href="misc/steffler/MakingSmalltalk-Article3.st">MakingSmalltalk-Article3.st</a>
|
|
code to your image (<a href="../issue60/steffler.html#puttingItAllTogether">see
|
|
article 2 on how to do this</a>).
|
|
Then open the browser by doing the snippet:
|
|
<p><i>[ex9] [Squeak-only-suspected]</i>
|
|
<br><b> ScopedBrowser openBrowserForArticle3</b>
|
|
<p> For the read-along folks, this is what you'll see
|
|
after navigating to the <b>withAll:</b> method:
|
|
<br><img SRC="misc/steffler/scopedBrowser1.png" height=283 width=582>
|
|
<br><font size=-1> <i>(Note: I set my browser colour
|
|
to purple - the default colour is green, I'll come back to customization
|
|
in a future article)</i></font>
|
|
<br> To find the <b>withAll:</b> method, click on the
|
|
<b>class</b>
|
|
button, then <b>Collections-Abstract>Collection>instanceCreation>withAll:</b>
|
|
<br> This browser has 5 panes and 3 buttons, from left
|
|
to right and top to bottom:
|
|
<br> pane 1: shows categories
|
|
- these are collections of classes <i>(pun intended)</i>
|
|
<br><i> </i>pane 2: shows classes
|
|
<br> pane 3: shows categories
|
|
- these are collections of methods
|
|
<br> pane 4: shows methods
|
|
<br> pane 5: shows Smalltalk
|
|
code
|
|
<br> button 1: toggles the browser
|
|
to show the instance methods of the object
|
|
<br> button 2: toggles the browser
|
|
to show the class comments
|
|
<br> button 3: toggles the browser
|
|
to show the class methods of the object
|
|
<br> Now, if we step back a little bit, and click on
|
|
<b>Collections-Sequenceable>OrderedCollection</b>,
|
|
you'll see:
|
|
<br><img SRC="misc/steffler/scopedBrowser2.png" height=284 width=581>
|
|
<br> Note that the code pane shows who <b>OrderedCollection</b>
|
|
inherits from, as well as their instance variables, if you then go back
|
|
to the abstract classes and click on <b>SequenceableCollection</b>, you'll
|
|
notice that it inherits from <b>Collection</b> just as we discussed.
|
|
Take some time poking around these classes and get comfortable with navigating
|
|
in this browser. Look for the classes and methods we discussed above.
|
|
<br> Finally, I'm going to introduce one more browser
|
|
- the hierarchy browser. This one is good when you're concentrating
|
|
on hierarchies and inheritence when you're coding. To open it, first
|
|
click on <b>OrderedCollection</b> again, then <b>middle-click>spawn hierarchy</b>.
|
|
You'll see:
|
|
<br><i>[ex10]</i>
|
|
<br><img SRC="misc/steffler/hierarchyBrowser1.png" height=318 width=537>
|
|
<br> Note, that this browser hasn't been scoped, and
|
|
shows the full hierarchy. Notice that Collection inherits from an
|
|
object called <b>Object</b> - no surprise here, most things about Smalltalk
|
|
are just what you would expect. Finally, the topmost object is <b>ProtoObject</b>,
|
|
which implements some really fundamental methods. The question naturally
|
|
arises: "What does <b>ProtoObject</b> inherit from?". The answer
|
|
is nothing, or <b>nil</b> to be more precise.
|
|
<h2>
|
|
<a NAME="lookingForward"></a>Looking forward</h2>
|
|
The next article will cover the making of aPerson; we'll
|
|
go over not only the basics of creating objects, but we'll also discuss
|
|
what the Smalltalk code that we're writing means.
|
|
<h2>
|
|
<a NAME="sweetSqueak"></a>A Sweet Squeak</h2>
|
|
This section won't explore/explain code or example, but merely present
|
|
a neat thing to try out. This time, we're going to look at downloading
|
|
projects. Remember in <a href="../issue59/steffler.html#firstLooks">article
|
|
1</a> when we created a new project? Well, you can not only save the project
|
|
to transfer across images, but you can drop it on the internet and download
|
|
it directly to your image too. Here's an example, do this:
|
|
<p><i>[ex11] [Squeak-only-suspected]</i>
|
|
<br><b> Project thumbnailFromUrl:
|
|
'http://www.squeak.org/Squeak2.0/2.7segments/SqueakEasy.extSeg'</b>
|
|
<p> For the read-along folks, you'll see a simple turtle
|
|
game project, and when you enter the project you can direct the turtle
|
|
by entering Smalltalk code:
|
|
<br><img SRC="misc/steffler/turtleGame1.png" height=479 width=639>
|
|
<h2>
|
|
<a NAME="questionsAndAnswers"></a>Questions and Answers</h2>
|
|
These are the answers for questions on previous articles that I could work
|
|
through in my limited time available. I picked out the ones I thought
|
|
most appropriate for the series. If you want a faster response, or
|
|
I didn't get to your question, try posting your question to the comp.lang.smalltalk
|
|
newsgroup, or the <a href="http://minnow.cc.gatech.edu/squeak/1">Swiki</a>.
|
|
<p><b>Q: How compatible with [VisualWorks, VisualAge, Smalltalk/X, Dolphin,
|
|
etc] Smalltalk will the code examples be?</b>
|
|
<br>A: Though I'm not writing these articles with code portability
|
|
in mind, and I'm not doing any portability testing, much of the basic code
|
|
should be compatible. By basic code, I mean things like how collections
|
|
are used, how classes are declared, instance variable use, etc. Traditionally
|
|
where the different flavours of Smalltalk differ most is in GUI code.
|
|
With Squeak specifically, some of the cool stuff we're going to look at
|
|
isn't portable to other flavours, for example: the halo stuff, morphic
|
|
stuff, and downloading projects.
|
|
<br> What I'll start doing though, is any code that I
|
|
a priori suspect is Squeak specific, I'll tag with<b> </b><i>[Squeak-only-suspected]</i><b>.
|
|
NOTE: </b>this will only indicate my <i>suspicion</i> - I don't plan
|
|
on spending time on testing it in different flavours, or searching for
|
|
ways to accomplish the same task in a different manner.
|
|
<br> This would be a great use of the Linux Gazette's
|
|
talkback sections - if other Smalltalkers note what does and doesn't work
|
|
in other flavours, they can post this info. Also starting with this
|
|
article, I'll start indexing the examples so they're easier to refer to
|
|
for this purpose (ie: ex1, ex2). I haven't done this yet, as I wanted
|
|
to keep the series informal, but I expect enumerating examples will make
|
|
it easier/clearer to post talkbacks. If you don't like the enumerating
|
|
- post a talkback.
|
|
<h2>
|
|
<a NAME="articleGlossary"></a>Article Glossary</h2>
|
|
This is a glossary of terms that I've used for the first time in this series,
|
|
or a term that I want to refine. <!-- If you don't see a term defined
|
|
here, try the ongoing glossary in the local location: <a href="../ongoingSeriesGlossary.html">[LL].</a -->
|
|
<br><b>abstract class</b>
|
|
<br> A class that would never
|
|
instantiate an object itself, but serves as good logical building point
|
|
for other classes that do instantiate themselves (concrete classes).
|
|
Abstract classes are indicated <i>in italics</i> in class outlines.
|
|
<br><b>class method</b>
|
|
<br><b> </b>(def1 - simple) A
|
|
method that a class implements (as opposed to an instance), usually to
|
|
create a new object. I denote a class method by: >>aClassMethod
|
|
<br><b>concrete class</b>
|
|
<br> A class that would instantiate
|
|
an object itself.
|
|
<br><b>instance method</b>
|
|
<br><b> </b>(def1 - simple) A
|
|
method that an instance of an object implements (as opposed to it's class).
|
|
I denote an instance method by: >anInstancemethod
|
|
<br><b>polymorphism</b>
|
|
<br><b> </b>When two different
|
|
objects interpret the same message differently by implementing two different
|
|
methods.
|
|
<br><b>receiver</b>
|
|
<br> The receiver of a message
|
|
send. The object that receives a message send. For example,
|
|
if we say <b>aPerson sing</b>, the receiver of the <b>sing</b> message
|
|
is <b>aPerson</b>
|
|
<br><b>[Squeak-only-suspected]</b>
|
|
<br><b> </b>This tag is for any code
|
|
that I a priori suspect is Squeak specific<b>. NOTE: </b>this
|
|
will only indicate my suspicion - I don't plan on spending time on testing
|
|
it in different flavours, or searching for ways to accomplish the same
|
|
task in a different manner.
|
|
<h2>
|
|
<a NAME="footnotes"></a>Footnotes</h2>
|
|
[1] For those folks who have programming experience, the parameter that
|
|
we're passing here is an <b>Array</b>, you can just highlight the array
|
|
and inspect it too if you like. The reason I'm not using an <b>Array</b>
|
|
for illustrative purposes here, as because they are not as flexible or
|
|
user friendly as an <b>OrderedCollection</b>. Where arrays have a
|
|
fixed size, an ordered collection grows or shrinks as you need it to.
|
|
<br>[2] For those folks never lucky enough to be in a comp sci class that
|
|
studies heaps and stacks and other data structures adnauseam, a heap
|
|
is a type of binary tree in which the value of each node is greater than
|
|
the values of its leaves.
|
|
<p>
|
|
|
|
<h2>
|
|
<a NAME="statementOfPurpose"></a>Statement of purpose</h2>
|
|
When I wrote the first Making Smalltalk with the Penguin
|
|
<a href="../issue51/steffler.html">article
|
|
back in March of 2000</a>,
|
|
my target audience was experienced programmers who didn't have much exposure
|
|
to <a href="#articleGlossary">OO</a> programming or to Smalltalk.
|
|
The article's intent was to give an overview of my favourite programming
|
|
language on my favourite operating system. Since then, I've had a
|
|
fair amount of email asking introductory type questions about Smalltalk
|
|
and OO programming. So I thought I'd try my hand at a small series.
|
|
<br> The target audience for this series are people new
|
|
to OO or new to programming altogether. The intent is to not only
|
|
introduce OO programming, but to also spread the fun of Smalltalking.
|
|
Why do this format/effort when there's lots of good <a href="http://minnow.cc.gatech.edu/squeak/377">reference
|
|
material out there</a>? Two reasons really: 1) Tutorials are
|
|
great, but can be static and dated pretty quickly. 2) An ongoing
|
|
series tends to be more engaging and digestible.
|
|
<br> To help address the second reason above, my intent
|
|
is to keep the articles concise so they can be digested in under an hour.
|
|
Hopefully, as newbies follow along, they can refer back to the original
|
|
article and make more sense of it. I plan on having a touch of advanced
|
|
stuff once in a while to add flavour and as before, the articles are going
|
|
to be written for read-along or code-along people.
|
|
<!-- br> Something new I'm going to try is to make the ongoing
|
|
series viewable in a contiguous fashion and downloadable in one chunk for
|
|
people who want to browse the series locally. To do this, click on
|
|
TOC grapic to at the top of the article. The articles also have 2
|
|
sets of links: one set for www links, another set for local links,
|
|
indicated as: <a href="#articleGlossary">[LL]</a> for downloaded local
|
|
reading.-->
|
|
<h2>
|
|
<a NAME="whySmalltalk"></a>Why Smalltalk?</h2>
|
|
I believe Smalltalk is <b><u>the best</u></b> environment
|
|
to learn OO programming in because:
|
|
<ul>
|
|
<li>
|
|
Smalltalk has a very active and very helpful community; when you post a
|
|
question to the Smalltalk newsgroups you very often get an answer, unlike
|
|
many other newsgroups</li>
|
|
|
|
<li>
|
|
is very easy to learn... one of it's original design intentions was to
|
|
be a learning environment for children</li>
|
|
|
|
<li>
|
|
is a pure OO environment and encourages OO programming (as opposed to encouraging
|
|
procedural/Object mixed programming)</li>
|
|
|
|
<li>
|
|
cutting your teeth in Smalltalk makes you a better OO programmer in any
|
|
other language, because of the previous bullet</li>
|
|
|
|
<li>
|
|
is a portable environment: write once, run anywhere, so people can
|
|
learn on whatever OS they're running (as opposed to just the M$ variety)</li>
|
|
|
|
<li>
|
|
can look at and manipulate objects in real time; I haven't seen this ability
|
|
in any other environment</li>
|
|
|
|
<li>
|
|
Smalltalk is written in Smalltalk. You can view how the language
|
|
is put together to learn the language, and you can change anything that
|
|
you don't like about it.</li>
|
|
|
|
<li>
|
|
has garbage collection, no manual memory management, no explicit pointers</li>
|
|
|
|
<li>
|
|
is a literate language; by this I mean the syntax is very simple and is
|
|
geared towards programmer readability.</li>
|
|
|
|
<li>
|
|
there's lots of Cool Things that you can do in it that I haven't seen anywhere
|
|
else (will have some examples along the way)</li>
|
|
|
|
<li>
|
|
...and best of all: <b><u>it's fun</u></b>.</li>
|
|
</ul>
|
|
In particular, I'm going to use <a href="http://www.squeak.org/">Squeak</a>
|
|
as the playing vehicle. You'll notice this is a different flavour
|
|
of Smalltalk than I used in my first article. I've never used Squeak
|
|
before, so this'll be a learning experience for me too. The reasons
|
|
for this are:
|
|
<ul>
|
|
<li>
|
|
It's a completely opensource project</li>
|
|
|
|
<li>
|
|
It has some Really Cool features that I haven't seen in other flavours
|
|
of Smalltalk</li>
|
|
|
|
<li>
|
|
It has a comparitively small footprint and it's very easy to install</li>
|
|
|
|
<li>
|
|
It has a strong <a href="http://minnow.cc.gatech.edu/squeak/1">Swiki site</a>
|
|
(a <a href="#articleGlossary">Wiki</a> site hosted in Squeak, hence <b>S</b>queak
|
|
<b>Wiki</b>)</li>
|
|
</ul>
|
|
|
|
|
|
<h2>
|
|
<a NAME="personSampleCode"></a><b>Smalltalk Code</b></h2>
|
|
No listings this month
|
|
|
|
|
|
|
|
|
|
<!-- *** BEGIN copyright *** -->
|
|
<P> <hr> <!-- P -->
|
|
<H5 ALIGN=center>
|
|
|
|
Copyright © 2000, Jason Steffler.<BR>
|
|
Copying license <A HREF="../copying.html">http://www.linuxgazette.com/copying.html</A><BR>
|
|
Published in Issue 61 of <i>Linux Gazette</i>, January 2001</H5>
|
|
<!-- *** END copyright *** -->
|
|
|
|
<!--startcut ==========================================================-->
|
|
<HR><P>
|
|
<CENTER>
|
|
<!-- *** BEGIN navbar *** -->
|
|
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="padala.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue61/steffler.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="ward.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
|
|
<!-- *** END navbar *** -->
|
|
</CENTER>
|
|
</BODY></HTML>
|
|
<!--endcut ============================================================-->
|