The Design of Smalltalk - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

The Design of Smalltalk

Description:

Smalltalk with Style guidelines for writing code that complies with ... See Chapter 5 of App Dev Guide for more info. Introduction to Smalltalk. 42. Variables ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 48
Provided by: joche1
Category:

less

Transcript and Presenter's Notes

Title: The Design of Smalltalk


1
Introduction to Smalltalk
  • The Design of Smalltalk
  • Understanding Code

2
Documentation
  • Smalltalk with Style guidelines for writing
    code that complies with the common idioms of
    Smalltalk (on-line)
  • Visualworks
  • Application Developers Guide
  • Basic Libraries
  • GUI Developers Guide

3
Sample Code
  • factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial

4
Sample Code
  • factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • The Method Name

5
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • A Comment

6
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • A String

7
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • Referring to the object itself

8
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • Periods separate statements (can skip on last
    line)

9
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • Return the value from the method

10
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • Blocks

11
The Design of Smalltalk
  • Smalltalk is really simple
  • It was meant to be
  • In its simplicity lies its greatness
  • We aim to make simple things simple and complex
    things possible. Alan Kay
  • Foundations of Smalltalk
  • Everything is an object
  • Class-based inheritance
  • All computation is done through message passing
  • Modeled on English (e.g., periods end statements)

12
Everything is an Object
  • There are no exceptions to the object rule.
  • Numbers, Booleans, etc.
  • nil is an object (it is the only instance of
    ProtoObject, the root of the class inheritance
    tree)
  • Even every class is an object
  • Blocks and methods are objects too

13
Class-Based Inheritance
  • Smalltalk is the Canonical Class-Based
    Object-Oriented Language
  • Every object is an instance of some class
  • All classes (except ProtoObject) have a parent
    class
  • Parent is superclass
  • Child is subclass
  • Subclasses inherit behavior and structure from
    parent class

14
Class Hierarchy SmallInteger
  • Magnitude
  • Character
  • Date
  • Number
  • Float
  • Fraction
  • Integer
  • SmallInteger
  • LargePositiveInteger
  • LargeNegativeInteger
  • Time

15
Lets Make a Class
16
Message Passing
  • All computation is triggered through message
    sends (sending a message to an object)
  • Messages trigger methods
  • Almost all of Smalltalk is ltreceiverObjectgt
    ltmessagegt
  • 5 factorial
  • 5 is the receiver object, factorial is the
    message
  • 5 3
  • 5 is the receiver object, is the message, 3 is
    an argument
  • How do we get from the message to the method?

17
Message Passing
  • Answer Through the Class Hierarchy
  • Class determines behavior.
  • Check the class of the receiver object for the
    method. If not found, check the parent. Go
    through the class hierarchy until found or return
    a does not understand error.
  • 5 factorial
  • 5 is a SmallInteger. Its parent (Integer)
    implements factorial.
  • 5 3
  • 5 is a SmallInteger, which implements

18
Message Passing
  • There can be more than one method implementing
    the same message
  • Which method gets executed is based on the class
    of the receiver
  • Methods in Object (like printString) can be sent
    to any object
  • The decision is made at runtime
  • This is late-binding
  • More than one kind of object can respond to the
    same message in its own way
  • 5 3 and 5.1 3.2 are very different methods
  • This is polymorphism

19
Message Passing
  • All computation is triggered through message
    passing
  • Even control structures are handled as message
    sends

20
Control Structures
  • test ifTrue Execute if test is true
  • test ifTrue Execute if test is true
  • ifFalse Execute if test is false
  • 1 to 10 do i Iterate for i equals 1 to
    10
  • test whileTrue Repeat this until test is
    false

21
Control Structures
  • test ifTrue Execute if test is true
  • 1 to 10 do i Iterate for i equals 1 to
    10
  • test whileTrue Repeat this until test is
    false
  • Receiver
  • Message (ifTrue ifTrueifFalse todo
    whileTrue)
  • Argument(s)

22
Control Structures
  • test ifTrue Execute if test is true
  • 1 to 10 do i Iterate for i equals 1 to
    10
  • test whileTrue Repeat this until test is
    false
  • Remember, everything is an object. Even blocks
    are objects
  • Boolean, False, and True implement ifTrue
  • Number implements todo
  • BlockContext implements whileTrue

23
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • Receiver Message Argument

24
Compiler and Blocks
  • Compiler is always available (and is of course an
    object)
  • Compiler evaluate 3 4 returns 7
  • Blocks Code elements that are objects
  • aBlock Transcript show foo. blocks can
    be assigned to variables
  • aBlock value. do it
  • Blocks can take arguments
  • asArgumentBlock x x1.
  • anArgumentBlock value 5. returns 6

25
3 timesRepeat Transcipt showfoo
  • (Integer) timesRepeat aBlock
  • Evaluate the argument, aBlock, the number of
    times represented by the receiver.
  • count
  • count 1.
  • count lt self test for while
  • whileTrue
  • aBlock value. evaluate block
  • count count 1 increment count

26
Message Passing
  • Message Names are Objects (Symbols)
  • factorial todo whileTrue
  • You can use them like any other object (save them
    to a variable, etc.)
  • The following evaluate the same
  • 5 factorial
  • 5 perform factorial
  • 5 perform perform with factorial

27
Kinds of Operators(Ways to Send Messages)
  • Binary
  • - / // quotient \\ remainder or mod
    do not short-circuit
  • Unary
  • abs not sin positive asUppercase
  • Keywords with
  • aDictionary at aKey put aValue
  • todo ifTrueifFalse
  • Order of precedence (not algebraic)
  • (First to Last) Parenthesis, Unary, Binary,
    Keyword
  • First come First served

28
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • Unary Operators

29
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • Binary Operators

30
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • Keyword Selectors

31
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • Figure out the precedent

32
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • 1st Parenthesis

33
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • 2nd Unary (Receiver)

34
Sample Code
  • (Integer) factorial
  • Returns the factorial of this integer.
  • 5 factorial evaluates to 120.
  • self lt 0 ifTrue self error Not defined.
  • self 0 ifTrue 1.
  • self (self - 1) factorial
  • 3rd Binary (Receiver) (Argument)

See Application Developers Guide, Chapter 4
35
One More Time
  • a string changeFrom 6 2 negated to 3 with
    (a a)

a string changeFrom 6 2 to 3 with (a
a) a string changeFrom 4 to 3 with (a
a) 'a saaing'
36
Class-Based Inheritance
  • Instances and classes do not respond to the same
    messages (Java and Smalltalk differ here)
  • In Java, instances will respond to class (static)
    methods.
  • In Smalltalk, they are separate. The class
    responds to a message with class methods. The
    instances respond to a message with instance
    methods.
  • In general, the Smalltalk separation is better
    for good object-oriented programming style. A
    class and its instance arent the same type of
    object therefore, they shouldnt share methods.
    Consider
  • rover Dog new.
  • rover name Rover.
  • brody rover new. Does that make sense?
  • brody rover class new. Thats better.

37
Class-Based Inheritance
  • Classes are instances too
  • Each class has its own metaclass
  • The class is an instance of that metaclass
  • Because of this, all class methods are actually
    instance methods
  • Again, Smalltalk is simple There is only one
    kind of method (instance methods)

38
Class-Based Inheritance
  • Accessing an Object
  • All instance variables are private
  • You cant just reach in and manipulate an object.
    Good OO Programming Ask. Dont Touch.
  • You can use simple accessors and mutators when
    appropriate to simulate public variables
  • All methods are public
  • If a object implements a message, you can send
    that message to the object and it will try to do
    it.
  • This can lead to errors, but Smalltalk deals with
    errors fairly well.
  • You will occasionally see comments or category
    names warning that a message is really private.

39
Modifying Classes and Methods
  • Trust the Programmer
  • In Smalltalk, you can modify any class or method.
  • You can create new methods on any class.
  • So, if you need to add a factorial capability,
    add it as a method to the Integer class. Thats
    where it belongs. Consider the following
  • FactorialProcessor factorialFor 5 not good OO
    code Make the object do the work.
  • 5 factorial
  • 5.0 factorial returns an error.

40
Creating a New ClassUse the SystemBrowser
  • Smalltalk defineClass NameOfClass
  • superclass NameOfSuperclass
  • indexedType none
  • private false
  • instanceVariableNames instVarName1
    instVarName2
  • classVariableNames classVarName1
  • imports
  • category categoryName

See Application Developers Guide, Chapter 5.
41
Creating a New ClassUse the SystemBrowser
  • NOTES
  • Namespaces help prevent name pollution.
    Everything is always accessible in Smalltalk, so
    traditionally, there was only one Point class.
    If I wanted to create a Point class, I would need
    to prefix it with the class name, like
    SudokuPoint. Now we can use namespaces to
    achieve a cleaner naming. It is similar to a
    package in Java.
  • Packages are ways to organize code in smalltalk,
    but have no effect on name visibility. The
    default package is Smalltalk.
  • Superclass of your object. Core.Object is the
    default. Same idea as in Java everything is
    subclass of Object.
  • Private if true, this class is only visible or
    usable by other classes in its namespace.
  • IndexedType always use none. See Chapter 5 of
    App Dev Guide for more info

42
Variables
  • foo bar Local Variables
  • foo 1. Assignment
  • Variables Any word starting with a letter
  • Start with lowercase, unless it is global
  • You dont declare variable type
  • All variables have a type (class of the object
    they point to)
  • Variables start as nil (type ProtoObject)

43
Variables
  • Variables point to objects
  • Objects with no references are garbage collected
  • You can have multiple variables point to the same
    object
  • a (1 2 3). An Array with 1, 2, and 3
  • b a.
  • a at 2 put 75.
  • b Returns (1 75 3)
  • Making copies (shallow) copy vs. deepCopy
  • Checking for sameness
  • equality objects have the same value
  • equivalence same object

44
CollectionsChoosing a Collection Class
  • Array (fixed size, ordered)
  • OrderedCollection (like Array, but can grow in
    size)
  • SortedCollection (OrderedCollection, but sorted
    according to block)
  • Dictionary (hash table)
  • Set (unordered, no redundancies)
  • Bag (count the number of pieces)

See Basic Libraries, Chapter 1.
45
Collections
  • Adding and removing elements
  • add addAll remove removeAll
  • Testing
  • isEmpty includes occurrencesOf
  • Enumerating
  • squeak do char Transcript show char
    printString
  • squeak select char char isVowel uea
  • (4 1 6) reject num num even (1)
  • (4 1 6) select num num odd (1)
  • (4 1 6) collect num num factorial (24 1
    720)
  • (4 1 6) detect num num even 4

46
Dictionaries
  • Has Key/Value pairs (like a hash table)
  • a Dictionary new.
  • a at orange put orange.
  • a at apple put red.
  • a at 5 put 678.
  • a keys. Set(orange 5 apple)
  • a values. OrderedCollection(678 orange red)
  • a at orange. orange
  • a keysAndValuesDo key value enumerating

47
Odds and Ends
  • Characters
  • Examples 1 a .
  • String is a collection of Characters
  • String concatenation (comma)
  • peanut butter, and , jelly peanut butter
    and jelly
  • Cascade (semicolon)
  • Sending multiple messages to the same object
  • Dog new
  • name Rover
  • breed Mutt
  • owner me.
  • Point
  • 2 _at_ 3 (or Point x 2 y 3)
Write a Comment
User Comments (0)
About PowerShow.com