old-www/LDP/LG/issue61/steffler.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>
&nbsp;&nbsp;&nbsp; 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.&nbsp; This month, we're going to discuss inheritence,
polymorphism, and abstract classes as well as introducing some collection
objects.&nbsp; 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>.&nbsp; 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.&nbsp;
This means that almost anyone can learn to write Smalltalk programs."
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - Peter William Lount
<h2>
<a NAME="inheritence"></a>Inheritence</h2>
&nbsp;&nbsp;&nbsp; Last month, we were using <b>aPerson</b> object for
illustrative purposes.&nbsp; This month we'll only build on this example
briefly, as it quickly gets too contrived for my liking.&nbsp; Also, this
is a good opportunity to introduce the collection objects of Smalltalk.&nbsp;
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>&nbsp;&nbsp;&nbsp; The notion of inheritence in OO is very similar
to the normal use of this term.&nbsp; Just like a person can inherit certain
characteristics from their parents, so too can objects inherit certain
characteristics from their parents.&nbsp; Objects inherit the methods and
instance variables of their parents, among other things.
<br>&nbsp;&nbsp;&nbsp; To get this discussion going, lets introduce the
object <b>anOrderedCollection, </b>whose class is <b>OrderedCollection</b>.&nbsp;
This is a very common object to use in Smalltalk.&nbsp; An <b>OrderedCollection</b>
is just what it sounds like:&nbsp; it's a collection of objects where order
is important.&nbsp; 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>&nbsp;&nbsp;&nbsp; "Inspect this code.&nbsp; 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>&nbsp;&nbsp;&nbsp; (OrderedCollection new)&nbsp; add: 1; add: 2;
add: 3; yourself.</b>
<p><i>[ex2]</i>
<br>&nbsp;&nbsp;&nbsp; "This is normally not written on one line, but is
written like this:"
<br>&nbsp;&nbsp;&nbsp; <b>(OrderedCollection new)</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add: 1;</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add: 2;</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add: 3;</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yourself.</b>
<p>&nbsp;&nbsp;&nbsp; Here, we're asking the class (remember, this is a
blueprint for creating objects) <b>OrderedCollection</b> to create a new
collection.&nbsp; Then we're asking the new collection to add 1 to itself.&nbsp;
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.&nbsp; 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.&nbsp; This may be a little confusing; I showed the above snippet
as it explicitly creates a new object.&nbsp; 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>&nbsp;&nbsp;&nbsp; "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>&nbsp;&nbsp; OrderedCollection with: 1 with: 2 with: 3</b>
<p><i>[ex4]</i>
<br>&nbsp;&nbsp;&nbsp; "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>&nbsp;&nbsp;&nbsp; <b>OrderedCollection withAll: #(1 2 3)</b>
<p><i>[ex5]</i>
<br>&nbsp;&nbsp;&nbsp; Now, if you <b>print it</b> to the above code, you'll
see an ASCII representation of the object:&nbsp; <b>OrderedCollection (1
2 3 )</b>.&nbsp; 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>&nbsp;&nbsp;&nbsp; There are several ways that we could make an ordered
collection with a fourth <b>Integer</b> in it, here's a neat way.&nbsp;
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.'&nbsp; 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>&nbsp;&nbsp;&nbsp; Highlight the code entered in the bottom pane, and
<b>do
it.&nbsp; </b>Here, you're asking the object itself (<b>self</b>) to add
4 to itself.&nbsp; 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.&nbsp; You'll see:
<br><img SRC="misc/steffler/orderedCollection3.png" height=199 width=249>
<p>&nbsp;&nbsp;&nbsp; This is an illustration of being able to view and
manipulate objects in real time, which is <b>Immensely Powerful</b>.&nbsp;
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.&nbsp; If you want to simulate certain conditions, <u>you can just change
the object directly</u>.&nbsp; 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>&nbsp;&nbsp;&nbsp; ...and remember, we did all this without the hassle
of compiling, linking, and running the compiled program!&nbsp; 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.&nbsp;
To do this, you can <b>do it</b> the following snippet:
<p><i>[ex8]</i>
<br><b>&nbsp;&nbsp;&nbsp; | anOrderedCollection aSum |</b>
<br><b>&nbsp;&nbsp;&nbsp; aSum := 0.</b>
<br><b>&nbsp;&nbsp;&nbsp; anOrderedCollection := OrderedCollection withAll:
#(1 2 3).</b>
<br><b>&nbsp;&nbsp;&nbsp; anOrderedCollection do: [:anElement | aSum :=
aSum + anElement].</b>
<br><b>&nbsp;&nbsp;&nbsp; aSum inspect.</b>
<p>&nbsp;&nbsp;&nbsp; Here, the lines of code mean:
<br>&nbsp;&nbsp;&nbsp; 1) declare temporary variables
<br>&nbsp;&nbsp;&nbsp; 2) initialize the sum
<br>&nbsp;&nbsp;&nbsp; 3) create a new ordered collection, assign it to
one of the temporary variables
<br>&nbsp;&nbsp;&nbsp; 4) ask the ordered collection to do something for
each element.&nbsp; For each element, we're asking the sum to add the element
to itself.
<br>&nbsp;&nbsp;&nbsp; 5) here, we're asking the sum to open an inspector
on itself (yeah, you can do this programatically - cool eh?)
<p>&nbsp;&nbsp;&nbsp; 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.&nbsp; Very nice and it helps to
reduce errors.&nbsp; We very naturally just asked the ordered collection
to <b>do</b> something with each element.
<br>&nbsp;&nbsp;&nbsp; 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.&nbsp; </b>To be more precise, it inherits from a class
called <b>SequenceableCollection</b>, which in turn inherits from <b>Collection.</b>&nbsp;
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>&nbsp;&nbsp;&nbsp; Collection</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SequenceableCollection</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
OrderedCollection</b>
<p>&nbsp;&nbsp;&nbsp; You can think of this as <b>OrderedCollection </b>is
a type of <b>SequenceableCollection</b>, which is a type of <b>Collection.</b>&nbsp;
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>&nbsp;&nbsp;&nbsp; Collection</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; >>withAll:</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SequenceableCollection</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
OrderedCollection</b>
<p>&nbsp;&nbsp;&nbsp; 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.&nbsp; 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>&nbsp; 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>&nbsp;&nbsp;&nbsp; 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>.&nbsp; Adding <b>Heap</b> to our outline, and indicating
abstract classes in italics gives us:
<br><b><i>&nbsp;&nbsp;&nbsp; Collection</i></b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; >>withAll:</b>
<br><b><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SequenceableCollection</i></b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
OrderedCollection</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Heap</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>>withAll:</b>
<p>&nbsp;&nbsp;&nbsp; Note:&nbsp; 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>&nbsp;&nbsp;&nbsp; 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.&nbsp; 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>&nbsp;&nbsp;&nbsp; Finally, if we also add the above mentioned <b>SortedCollection
</b>and<b>
Bag </b>(&lt;groan> here's where this month's title pun comes from ;-),
we get:
<br><b><i>&nbsp;&nbsp;&nbsp; Collection</i></b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; >>withAll:</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Bag</b>
<br><b><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SequenceableCollection</i></b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
OrderedCollection</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
SortedCollection</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Heap</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>>withAll:</b>
<p><b>&nbsp;&nbsp;&nbsp; </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.&nbsp;
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>&nbsp;&nbsp;&nbsp; Now we're going to start getting to the question
of how we know what objects are where and how to use them.&nbsp; As with
other topics in this series, I'm introducing this one a bit at a time as
well.&nbsp; 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.&nbsp;
To help reduce this problem, I've extended one of the Smalltalk browsers
and made a <b>ScopedBrowser</b>.&nbsp; 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.&nbsp; This <b>ScopedBrowser</b> will only show you
the classes we need to concentrate on for this article.&nbsp; My intent
is to add to the scope that is being browsed over time as more objects
are introduced.&nbsp; 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).&nbsp; 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>).&nbsp;
Then open the browser by doing the snippet:
<p><i>[ex9]&nbsp;&nbsp;&nbsp; [Squeak-only-suspected]</i>
<br><b>&nbsp;&nbsp;&nbsp; ScopedBrowser openBrowserForArticle3</b>
<p>&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp; <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>&nbsp;&nbsp;&nbsp; To find the <b>withAll:</b> method, click on the
<b>class</b>
button, then <b>Collections-Abstract>Collection>instanceCreation>withAll:</b>
<br>&nbsp;&nbsp;&nbsp; This browser has 5 panes and 3 buttons, from left
to right and top to bottom:
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pane 1: shows categories
- these are collections of classes <i>(pun intended)</i>
<br><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </i>pane 2: shows classes
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pane 3: shows categories
- these are collections of methods
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pane 4: shows methods
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pane 5: shows Smalltalk
code
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; button 1: toggles the browser
to show the instance methods of the object
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; button 2: toggles the browser
to show the class comments
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; button 3: toggles the browser
to show the class methods of the object
<br>&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp; 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.&nbsp;
Take some time poking around these classes and get comfortable with navigating
in this browser.&nbsp; Look for the classes and methods we discussed above.
<br>&nbsp;&nbsp;&nbsp; Finally, I'm going to introduce one more browser
- the hierarchy browser.&nbsp; This one is good when you're concentrating
on hierarchies and inheritence when you're coding.&nbsp; To open it, first
click on <b>OrderedCollection</b> again, then <b>middle-click>spawn hierarchy</b>.&nbsp;
You'll see:
<br><i>[ex10]</i>
<br><img SRC="misc/steffler/hierarchyBrowser1.png" height=318 width=537>
<br>&nbsp;&nbsp;&nbsp; Note, that this browser hasn't been scoped, and
shows the full hierarchy.&nbsp; Notice that Collection inherits from an
object called <b>Object</b> - no surprise here, most things about Smalltalk
are just what you would expect.&nbsp; Finally, the topmost object is <b>ProtoObject</b>,
which implements some really fundamental methods.&nbsp; The question naturally
arises:&nbsp; "What does <b>ProtoObject</b> inherit from?".&nbsp; The answer
is nothing, or <b>nil</b> to be more precise.
<h2>
<a NAME="lookingForward"></a>Looking forward</h2>
&nbsp;&nbsp;&nbsp; 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.&nbsp; This time, we're going to look at downloading
projects.&nbsp; Remember in <a href="../issue59/steffler.html#firstLooks">article
1</a> when we created a new project?&nbsp; 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.&nbsp; Here's an example, do this:
<p><i>[ex11]&nbsp;&nbsp;&nbsp; [Squeak-only-suspected]</i>
<br><b>&nbsp;&nbsp;&nbsp; Project thumbnailFromUrl:&nbsp;&nbsp;&nbsp;&nbsp;
'http://www.squeak.org/Squeak2.0/2.7segments/SqueakEasy.extSeg'</b>
<p>&nbsp;&nbsp;&nbsp; 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.&nbsp; I picked out the ones I thought
most appropriate for the series.&nbsp; 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:&nbsp; 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.&nbsp; By basic code, I mean things like how collections
are used, how classes are declared, instance variable use, etc.&nbsp; Traditionally
where the different flavours of Smalltalk differ most is in GUI code.&nbsp;
With Squeak specifically, some of the cool stuff we're going to look at
isn't portable to other flavours, for example:&nbsp; the halo stuff, morphic
stuff, and downloading projects.
<br>&nbsp;&nbsp;&nbsp; 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>.&nbsp;
NOTE:&nbsp; </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>&nbsp;&nbsp;&nbsp; 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.&nbsp; Also starting with this
article, I'll start indexing the examples so they're easier to refer to
for this purpose (ie: ex1, ex2).&nbsp; 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.&nbsp; 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.&nbsp; <!-- 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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).&nbsp;
Abstract classes are indicated <i>in italics</i> in class outlines.
<br><b>class method</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>(def1 - simple) A
method that a class implements (as opposed to an instance), usually to
create a new object.&nbsp; I denote a class method by: &gt;&gt;aClassMethod
<br><b>concrete class</b>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; A class that would instantiate
an object itself.
<br><b>instance method</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>(def1 - simple) A
method that an instance of an object implements (as opposed to it's class).&nbsp;
I denote an instance method by: &gt;anInstancemethod
<br><b>polymorphism</b>
<br><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>When two different
objects interpret the same message differently by implementing two different
methods.
<br><b>receiver</b>
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The receiver of a message
send.&nbsp; The object that receives a message send.&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </b>This tag is for any code
that I a priori suspect is Squeak specific<b>.&nbsp; NOTE:&nbsp; </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.&nbsp; 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>.&nbsp; 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&nbsp; 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>
&nbsp;&nbsp;&nbsp; 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.&nbsp;
The article's intent was to give an overview of my favourite programming
language on my favourite operating system.&nbsp; Since then, I've had a
fair amount of email asking introductory type questions about Smalltalk
and OO programming.&nbsp; So I thought I'd try my hand at a small series.
<br>&nbsp;&nbsp;&nbsp; The target audience for this series are people new
to OO or new to programming altogether.&nbsp; The intent is to not only
introduce OO programming, but to also spread the fun of Smalltalking.&nbsp;
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>?&nbsp; Two reasons really:&nbsp; 1) Tutorials are
great, but can be static and dated pretty quickly.&nbsp; 2) An ongoing
series tends to be more engaging and digestible.
<br>&nbsp;&nbsp;&nbsp; To help address the second reason above, my intent
is to keep the articles concise so they can be digested in under an hour.&nbsp;
Hopefully, as newbies follow along, they can refer back to the original
article and make more sense of it.&nbsp; 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>&nbsp;&nbsp;&nbsp; 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.&nbsp; To do this, click on
TOC grapic to at the top of the article.&nbsp; The articles also have 2
sets of links:&nbsp; 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>
&nbsp;&nbsp;&nbsp; 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:&nbsp; write once, run anywhere, so people can
learn on whatever OS they're running&nbsp; (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.&nbsp; 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.&nbsp; You'll notice this is a different flavour
of Smalltalk than I used in my first article.&nbsp; I've never used Squeak
before, so this'll be a learning experience for me too.&nbsp; 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>&nbsp;
(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 &copy; 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 ============================================================-->