[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 !).

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.

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 with:
   WorkspaceApplication open
and create a variable named "myPen" by selecting its "Add Workspace Variable" menu item:

and entering "myPen" into the appearing name dialog.
Then, in this (or another) workspace, evaluate the following code:
    | view |

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

    myPen := Pen in: view.
This creates and opens up another view. 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:
go: distance
makes the pen move forward the given number of pixels

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

To make the pen draw a rectangle, evaluate:
    myPen go: 50.
    myPen turn: 90.
    myPen go: 100.
    myPen turn: 90.
    myPen go: 50.
    myPen turn: 90.
    myPen go: 100.
    myPen turn: 90.

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 can be referred to by the 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 which 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
Browsers are explained in detail in the "Programming Tools" sections of this online document - for now, lets just find the Pen-class (use the "Find-Class" menu function) 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:
Can you spot the "go:" and "turn" messages we used before ?
With the help of the browser, have a look into the implementation and see what is going on there.

Find and try (in a workspace) the spiral and mandala messages.

Adding a new Method to the Pen Class

... to be continued ...


Continue in "Creating new Classes".


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

<cg@exept.de>

Doc $Revision: 1.1 $ $Date: 2003/11/25 19:20:49 $