[prev] [up] [next]

Playing with Objects and Classes

Smalltalk systems provide an integrated programming environment, which is way ahead of any other programming system, in that it is really integrated (*).

This programming environment allows up-to date access to an objects instance variables, to a classes definition, the set of messages which are implemented there (i.e. the protocol), the source code of those methods, the execution state and much more.
In addition, cross references can be searched (who sends which message, who implements which message, who references a variable etc.), breakpoints can be set and modifications can be made to a class.
Modifications to a class can be made at any time - even while there are active instances of it around; any modification is immediately effective (that does make a difference in programmers productivity !).

Drawing with a Pen

Let us use an instance of the Pen class for the demonstration.
Pens are objects which can draw onto a drawing surface, such as a view (window) on the screen.
Let us first create both a view and a pen, and assign them to workspace variables so that we can later refer to them by name.
First, open a Workspace via the Launchers menu or by evaluating:
   WorkspaceApplication open
and create a variable named "myPen" by selecting its "Add Workspace Variable" menu item:

and entering "myPen" into the appearing name dialog. Repeat this procedure for a "myView" workspace variable.
(alternatively, you can change the workspaces settings, to autmatically declare all unknown variables as workspace variables. See the "Workspace"-"Settings" menu for this.)

Then, in this (or another) workspace, evaluate the following code:


    myView := View new.
    myView width: 200.
    myView height: 200.
    myView open.

    myPen := Pen in: myView.
This creates and opens up another view (referred to by the name "myView"). In addition, it also creates a pen-instance and makes the workspace variable "myPen" refer to it.
Pens understands a bunch of messages, among them are:
home
positions the pen at the center of the window

go: distance
makes the pen move forward the given number of pixels

turn: degrees
makes the pen turn clockwise the given number of degrees

fillColor: aColorObject
changes the pens drawing color


Views understand hundreds of useful messages (there is a whole chapter on that in the documentation). For now, we only need to know a single message:
clear
clears the window

The clear message is useful to remove any of the pens previous drawings.

To make the pen draw a triangle, evaluate:

    myPen home.

    myPen go: 50.
    myPen turn: 120.
    myPen go: 50.
    myPen turn: 120.
    myPen go: 50.
    myPen turn: 120.
You get colored triangles with:
    myView clear.
    myPen home.
    myPen fillColor: (Color red).

    myPen go: 50.
    myPen turn: 120.
    myPen go: 50.
    myPen turn: 120.
    myPen go: 50.
    myPen turn: 120.

    myPen fillColor: (Color black).
to make it draw a nice picture, try (and understand):
    myView clear.
    myPen home.
    18 timesRepeat: [
	myPen go: 50.
	myPen turn: 120.
	myPen go: 50.
	myPen turn: 120.
	myPen go: 50.
	myPen turn: 120.

	myPen turn:20.
    ]
(notice the use of a block here, and the extra turn after the triangle has been draw).

Looking into our Pen

Occasionally, you may want to look inside an object - either for education, to understand what information is held privately within an object, or to find a bug, when wrong values are computed or held by an object.

To inspect the internals of an object, we use a so called Inspector, which can be opened by sending the object to be inspect an "inspect"-message.
Try:

    myPen inspect.
You will see (scroll down the left list if required) that - among other - the pen keeps information about its current position ("locationX" and "locationY") and its drawing direction ("direction"). For now, ignore the other values you will see in the inspector.

Clicking on such an instance variable in the inspector will display its value in the right part of the inspectors window.

It is also possible to interactively send messages to the inspected object from within the inspector, by entering the message expression into the inspectors (right) code pane, and evaluating it (using "doIt") just like in a workspace.

Within the inspector, the inspected object is referred to by the pseudo-name "self"; Therefore, we can send messages to the inspected object, even if no global- or workspace-variable refers to it.

As an excercise, let the pen draw something using self-messages from within an inspector.

Browsing Pens protocol

To see all messages that our pen understands, we can open a so called Browser on its class. A browser is a powerful tool both to find your way through the system (browsing) and to create or modify classes.
The browser operates on the class(es) as they are present in your running system - i.e. changes done there are immediately available. There is no need to leave the system for such changes.
There are many ways to open up a browser:
- via the Launcher
by pressing the browsers icon or selecting the "SystemBrowser" item in the classes menu.

- via a Workspace
by selecting an expressions text and then applying the "others"-"browseIt" popUp-menu function 1

- via an Inspector
the selector also provides a "browseIt" popUp-menu function for the selected private variable

- by evaluating an expression
the browser can also be opened by a program, by sending the "browse" message to a class or by sending an "open"-message to the browser-class.
Browsers are explained in detail in the "Programming Tools" section of this online document. For now, lets just find the Pen-class (use the "Find-Class" menu function in the browsers tool menu) and have a look at the Pen classes full protocol (select "*all*" in the protocol list, which is the second right list in the browsers upper area).

Excercises:

  1. Can you find the implementations of the "go:" and "turn"-messages (in the Pen class) we used before ?
  2. With the help of the browser, have a look into the implementation and see what is going on there.
  3. Find and try (in a workspace) the spiral- and mandala-messages.

Adding a new Method to the Pen Class

Lets assume, that we often have to draw triangles with our pen, and become bored of writing the above piece of code again and again.
We want that pens understand a "triangle"-message so that we can write:
    myPen triangle.
and have the pen draw a triangle with sides of 50 pixel length.
In order to teach Pens this new trick, perform the following steps:
  1. Open a browser and navigate to the Pen class.
  2. make sure you have the "instance"-toggle selected (this is explained later).
  3. select the protocol "geometric designs" in the second-right-top-pane.
Now, your browser window should look like:

In the text-area at the bottm, replace the shown text with the code for the triangle-method, as:

    triangle
	self go: 50.
	self turn: 120.
	self go: 50.
	self turn: 120.
	self go: 50.
	self turn: 120.
To install (compile) that new method, select the "accept" item of the text-areas popUpMenu:

The syntax is very simple: the first line defines the name of the new method (the selector), the rest are the statements to be evaluated, when a triangle message arrives at a pen. (in this case, a bunch of messages is sent to the receiver itself.

Now, try to send this new message to our old friend, "myPen" by evaluating (in a workspace):

    myPen triangle
Notice, that the pen class has been changed for a living object - we did not have to shutdown and restart any program. The existing pen instance learned the new message while being alive ! (that is what makes an integrated development environment.) Excercises:
  1. Add another Pen-method named "triangle:" (with colon), which takes a single argument and draws a triangle with sides of that length.
  2. Once the above "triangle:" method works, change the "triangle"-method (without colon) to invoke that new method. (such an action is called "refactoring" and is very common when working in Smalltalk)
  3. Add methods to draw squares and other geometric figures.
  4. Refactor your code by writing a central method (which takes the number of sides and side-length as arguments) and use that one as helper for the squares, triangle etc. methods.

... to be continued ...

Footnotes:
(*)
Integrated Environment:
In ancient times, this term meant that the programming environment was part of the executed program and vice versa.
The first integrated programming environments were provided by Lisp and Smalltalk systems.
Nowadays, by the help of a herd of marketing jerks, the meaning of "IDE" degenerated to: "a set of tools that can be started by a common launcher or are bundled together into one top window".

From the Smalltalk perspective, these systems are not "integrated" at all.


Continue in "Creating new Classes".


[stx-logo]
Copyright © Claus Gittinger Development & Consulting
Copyright © eXept Software AG

<cg@exept.de>

Doc $Revision: 1.4 $ $Date: 2004/09/29 12:04:00 $