SMALLTALK Lecturers Hesam Setareh Salar Moarref 1 Fall 2009 Programming languages Topics History and Motivation Structural Organization Classes and Subclasses Message Sending Implementation ID: 405207
Download Presentation The PPT/PDF document "OBJECT-ORIENTED PROGRAMMING" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
OBJECT-ORIENTED PROGRAMMINGSMALLTALK
Lecturers:Hesam SetarehSalar Moarref
1
Fall 2009 – Programming languagesSlide2
Topics
History and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementation
Object-Oriented extensionsEvaluation and Epilog
2Slide3
History and Motivation
Alan Kay at the University of Utah in the late 1960s, suggested that it is possible to put the power of a room-sized, million dollar computer into a package. (Personal Computer)Personal computer needs a personal programming language.
Older languages were designed for the scientific and commercial applications that occupy large computers.
Kay designed a simulation and graphic-oriented programming language for
nonspecialists
.
3Slide4
History and Motivation
Kay is a member of FLEX designer team. FLEX took the ideas of classes and objects from Simula.Kay wanted to provide a rich interactive environment for nonspecialists.
Xerox came in: In the 1971 Xerox produced a personal computer, called
Dynabook
.
4Slide5
The born of Smalltalk
Smalltalk first version, Smalltalk-72, was designed and implement for Dynabook by 1972.Smalltalk-74, Smalltalk-76, Smalltalk-78 , and Smalltalk-80 are other versions of it.
5Slide6
Topics
History and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementation
Object-Oriented extensionsEvaluation and Epilog
6Slide7
In Smalltalk everything is object, including variables, numbers, strings, etc. For example : x*2
Any object has two major parts:7
Properties (Variables)
Behaviors (Methods)
Structural OrganizationSlide8
Scribe goto:500@500.
8
ExampleSlide9
Scribe goto:500@500.
9
Scribe go:300.
ExampleSlide10
Scribe goto:500@500.
10
Scribe go:300.
Scribe turn:90.
ExampleSlide11
Scribe goto:500@500.
11
Scribe go:300.
Scribe turn:90.
Scribe go:300.
ExampleSlide12
Scribe goto:500@500.
12
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90.
ExampleSlide13
Scribe goto:500@500.
13
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90.
Scribe go:300.
ExampleSlide14
Scribe goto:500@500.
14
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90.
ExampleSlide15
Scribe goto:500@500.
15
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90.
Scribe go:300.
ExampleSlide16
Scribe goto:500@500.
16
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90
ExampleSlide17
Scribe goto:500@500.
17
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90.
Scribe go:300.
Scribe turn:90
4
timesRepeat
: [Scribe go:300. Scribe turn:90]
ExampleSlide18
Class
A class is a plan, which objects are made from.Getting a new object from a class is called instantiation.
18
anotherScribe
← pen newAt:200@200Slide19
shape||
scribe penup;goto:loc;turnTo:tilt;pendn. 4 timesRepeat: [scribe go:size;turn:90]show || scribe color ink. self shape
erase || scribe color background. self shape
grow: amount ||
self erase.
size
←
size+amount
.
self show
Class Definition
19
class name
box
Instance variable name
Loc tilt size scribe
Instance message and methodsSlide20
newAt:initialLocation|newBox
| newBox ← self new. newBox setLoc:initialLocation
tilt:0 size:100 scribe:pen new.
newBox
show.
↑
newBox
shape||
scribe
penup;goto:loc;turnTo:tilt;pendn
.
4
timesRepeat
: [scribe go:size;turn:90]
Class Definition
20
class name
box
Instance variable name
Loc tilt size scribe
Instance message and methods
class message and methodsSlide21
Topics
History and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementation
Object-Oriented extensionsEvaluation and Epilog
21Slide22
Classes and Subclasses
Smalltalk objects model Real-World!Some of objects have similar properties and behaviors.We can group classes of these objects and make superclass which has common variables and methods.
22
dispayObject
box
pen
windowSlide23
Classes and Subclasses
23
class name
box
Instance variable name
displayObject
Instance message and methods
superclass
Loc tilt size scribeSlide24
Classes and Subclasses
A Superclass can be subclass of another class.24
dispayObject
box
pen
window
number
…
Object
…Slide25
print ||
↑ realPt print + “+” imagePt print + “i”
Classes and Subclasses
25
class name
complex
Instance variable name
realPt
imagePt
Instance message and methods
Subclasses can change the inherent method, this process is called OverridingSlide26
An Important Issue
In Smalltalk a class can’t have several superclass.But, in the Real-World, an object can inherent from more than one object.
26
dispayObject
box
pen
bumper
number
inventoryItem
Object
roof
doorSlide27
An Important Issue
27dispayObject
brake
engine
bumper
number
inventoryItem
Object
roof
doorSlide28
A simple solution
28
28
dispayObject
box
pen
bumper
number
inventoryItem
Object
roof
door
…
!Slide29
African
S.American
N.American
Another aspect of that issue
The way of classification causes class grouping.
It is possible for some classes to have several ways of classification.
Orthogonal classification:
29
Pets
Beasts of burden
Source of food
PestsSlide30
Multiple Inheritance raises difficult problem
Multiple inheritance allows a class to be an immediate subclass of more than one class.It solve one problem but, brings many new problems.
30
C
B
ASlide31
Topics
History and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementation
Object-Oriented extensionsEvaluation and Epilog
31Slide32
Dynamic versus Static, Strong versus weak
Smalltalk uses dynamic type checking like LISP.Pascal and Ada use static type checking.Dynamic type checking does not imply weak type.32Slide33
Forms of Message Template (Argument Passing)
Message sending syntaxes:Object_name methodnameObject_name methodname : argument
Object_name methodname : 1
st
arg
2
nd
arg_name:2nd
arg_val
We have a problem with arithmetic expression: (x+2)*y (x plus : 2) times : y
It became possible for one-parameter message to use an operation.
33Slide34
run ||
Keyboard read eval printThe Smalltalk main loop is written in Smalltalk
34
class name
userTask
Instance message and methods
Smalltalk is in a loop: read a command, execute the command, print the result and loop
true
whileTrue
: [Display put: user run]Slide35
run ||
sched map: [: Task | Task run]Concurrency is easy to implement
35
class name
scheduler
Instance message and methods
Sched
is the name of a set that contains all of the objects that are scheduled to be run concurrently.
S map: B, apply block B to every element of S. Slide36
Topics
History and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementation
Object-Oriented extensionsEvaluation and Epilog
36Slide37
Implementation: Classes and objectsMost of Smalltalk system is written in
smalltalkCompiler, decompiler, debugger, editors,… 97% most of implementation data structures are smalltalk objectsSmalltalk-80
VM:not portable6~12KB assembly codeOne man-year to produce a fully
debuged
version
37Slide38
ImplementationSmalltalk Virtual machine:
Storage managerEncapsulate the representation of objects and organization of memory Fetch the class of an objectFetch and store the fields of objectsCreate new objectsInterpreterPrimitive subroutines
38Slide39
ImplementationSmalltalk Virtual machine:
Storage managerInterpreter Manager for methodsPrimitive subroutinesCollection of methodsFor performance reasons are implemented in machine code basic I/O functions, integer arithmetic, basic screen graphics functions
39Slide40
Implementation There are three central ideas in Smalltalk
ObjectsClassesMessage sending40Slide41
Object representationRepresentation of an object must contain just that information that varies from object to object
Instance variables The information stored with the class includes the class methods and instance methods41Slide42
Representation of objects
Len6c.d.
locTilt
Size
scribe
Len
6
c.d.
Loc
Tilt
Size
Scribe
Len
4
c.d.
X
Y
Box
500@600
200
500
point
42Slide43
Class representation everything in Smalltalk is an object
Even classes! Classes are instances of the class named classInstances variables of a class object contain pointers to objects representing the information that is the same for all instances of the classWhat is this information?43Slide44
Representation of class object
Len8c.d.
nameSuper class
Inst.
Vars
Class
msgs
.
Inst.
Msgs
.
Inst. Size
4
class
“box”
DisplayObject
“loc tilt size scribe”
Message
dict
Message
dict
44Slide45
Representation of class object(continued)
inst. Size number of instance variables Needed by storage manager when it instantiates an objectMessage dictionariesA method identified by the keywords
Scribe go:100 go: identifies the methodSpinner newAt
: 500@200 rate:1
newAt:rate
: is method
Message template
For each message template acceptable to a class or its instances, one of the message dictionaries must contain an entry
45Slide46
Message dictionary How should method be represented?
Too much slow if the interpreter had to decode the source form every time the method was executedCompile the code into some form of pseudo code that can be rapidly interpretedThe source form is needed for editing and displaying class definitionMessage dictionaries contain two entries for each message template
46Slide47
Example of message dictionary
. . .
msgmethod
source
.
.
.
Message dictionary
“grow:”
“grow: amount || self release.
Size<-
size+amount
. Self show”
Push ‘
slef
’
Send ‘erase’,0
Pop
Push ‘size’
Push ‘size’
Push ‘amount’
Send ‘+’,1
Send ‘<-’,1
pop
method
47Slide48
Implementation of message sendingThere is a strong resemblance between message passing in Smalltalk and procedure calls in other languages
Activation records: primary vehicle for procedure implementationThe same is the case in SmalltalkActivation records hold all of the information relevant to one activation of a method 48Slide49
Structure of an activation recordEnvironment part
The context to be used for execution of the methodInstruction partThe instruction to be executed when this method is resumedSender partThe activation record of the method that sent the message invoking this method49Slide50
Instruction part Must designate a particular instruction in a particular method
Methods are themselves objects instances of class methodTwo coordinate system is used for identifying instructions:An object pointer defines the method-objectA relative offset identifies the particular instruction within the method object
50Slide51
Environment partLocal environment
Parameters to methodTemporary variablesHidden temporary variables such as the intermediate results of expressionsNonlocal environment: all other visible variablesInstance variablesClass variables
51Slide52
Parts of an activation record
localsparameters
Temporary vars
intermediates
Non locals
Static link
Instruction part
method
instruction
Sender part
Dynamic link
Length
c.d.
Instance
vars
instance
Length
c.d.
Superclass
Class variable
Class
Length
c.d.
Superclass
Super class variables
52Slide53
Message sending create an activation record for the receiver (
callee)Identify the method being invoked by extracting the template form from the message and then looking it up in the message dictionary for the receiving object’s class or super classTransmit the parameters to the receiver’s activation recordsSuspend the sender(caller) by saving its state in its activation record.Establish a path(dynamic link) from the receiver back to the sender and establish the receiver’s activation record as the active one
53Slide54
Returning from a method transmit the returned object(if any) from the receiver back to sender
Resume execution of the sender by restoring its state from its activation record54Slide55
Returning from a method transmit the returned object(if any) from the receiver back to sender
Resume execution of the sender by restoring its state from its activation record deallocation of the activation record???The storage manager handles allocation and deallocation of all objects, this include AR objectsWe don’t explicitly deallocate
activation records Smalltalk doesn’t allocate its activation records on a stackconcurrency
55Slide56
Topics
History and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementation
Object-Oriented extensionsEvaluation and Epilog
56Slide57
Object oriented extensions Many languages have been extended for object oriented programming
Fifth generation languages such as LISPThird and fourth generation languages such as C and Ada83Simula-67 is essentially an object-oriented extension to algol-60Programming by extensionSuggested by object oriented languagesAbility to extend and refine software systems without needing to recompile and retest the parts already implemented
57Slide58
Tagged types support programming by extension-Ada
Package Display_Objects is type display_object is tagged [private] record loc: display_location;
end record;Procedure Go_to ( The_object
:
display_object
;
new_loc
: display_location
);
Procedure Show (
The_object:display_object
);
Procedure Erase (
The_object:display_object
);
End
display_objects
;
58Slide59
Tagged types support programming by extension-Ada
With display_objects;Package Boxes is type Box is new display_objects.display_object with [private] record tilt, size :float;
The_scribe: scribe; end record;
procedure shape (
The_Box
: Box);
.
.
.
End Boxes;
59Slide60
Package
All_Display_Objects type Display_Object is tagged private;
… operations for Display_Object
…
type Box is new
Display_Object
with private;
… operations for Box …
type Window is new
Display_Object
with private;
… operations for Window …
… etc. …
end
All_Display_Objects
60
Types and Their Refinements Can be Defined is a Single PackageSlide61
Abstract declarations in
display_object:Procedure shape (The_object: display_Object) is abstract;In Box:Procedure shape (The_box:Box
); In package body for display_objects:procedure show(
The_object
:
display_object
) is
Begin
Set_color
(
The_object
);
Shape(
The_object
);
End show;
61Slide62
Ada’s support for object oriented programming is complex
The complexity comes from grafting the object oriented features onto a type system that was designed for different trade-offs(e.g. efficiency on small embedded computers)62Slide63
C++ extends C for object-oriented programmingC++ is largely the work of
Bjarne StroustrupC++ supports:Class declarationMultiple inheritanceVirtual abstract in Ada
Template generic in Ada
63Slide64
Java is a more advanced object-oriented language derived from CDesigned for networked and distributed programming environments
Greater emphasis on security, robustness, machine independence, portabilityJava emits some C++ features: operator overloading, pointers,…Add others such as automatic garbage collection64Slide65
Topics
History and MotivationStructural OrganizationClasses aMessage Sendingnd Subclasses
ImplementationObject-Oriented extensions
Evaluation and Epilog
65Slide66
Evalution and epilogSmalltalk is small, flexible, and extensible
Small, simple, regular languageSmall number of independent concepts easy to learnHas demonstrated its flexibility by being Used in a number of applications: simulations, graphics, artificial intelligenceDesign of Smalltalk has been dictated by several powerful principles: simplicity, regularity, abstraction, security and information hiding
66Slide67
Evalution and epilogSmalltalk is an example of a programming environment
Has introduced important ideas other than language itself, like widowsIs also an excellent example of an advanced programming environmentIntegration of graphics, windows and an advanced language67Slide68
Evalution and epilogSmalltalk introduces a new programming paradigm
Although most of its ideas have appeared in other languages, smalltalk’s integration of these ideas is novelThe essence of the Smalltalk view is that programming is simulation programs model, sometimes in a very abstract form, some aspects of the real world
68Slide69
Evalution and epilog
69Slide70
The End
70