760 likes | 1.01k Views
OBJECT-ORIENTED PROGRAMMING SMALLTALK. Lecturers: Hesam Setareh Salar Moarref. Fall 2009 – Programming languages. Topics. History and Motivation Structural Organization Classes and Subclasses Message Sending Implementation Object-Oriented extensions Evaluation and Epilog.
E N D
OBJECT-ORIENTED PROGRAMMINGSMALLTALK Lecturers: HesamSetareh SalarMoarref Fall 2009 – Programming languages
Topics • History and Motivation • Structural Organization • Classes and Subclasses • Message Sending • Implementation • Object-Oriented extensions • Evaluation and Epilog
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.
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.
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.
Topics • History and Motivation • Structural Organization • Classes and Subclasses • Message Sending • Implementation • Object-Oriented extensions • Evaluation and Epilog
Structural Organization • In Smalltalk everything is object, including variables, numbers, strings, etc. • For example : x*2 • Any object has two major parts: Properties (Variables) Behaviors (Methods)
Example Scribe goto:500@500.
Example Scribe goto:500@500. Scribe go:300.
Example Scribe goto:500@500. Scribe go:300. Scribe turn:90.
Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300.
Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90.
Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300.
Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90.
Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300.
Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90
Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. 4 timesRepeat: [Scribe go:300. Scribe turn:90] Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90
Class • A class is a plan, which objects are made from. • Getting a new object from a class is called instantiation. anotherScribe ← pen newAt:200@200
Class Definition class name box Instance variable name Loc tilt size scribe 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 Instance message and methods
Class Definition class name box Instance variable name Loc tilt size scribe newAt:initialLocation|newBox| newBox← self new. newBoxsetLoc:initialLocation tilt:0 size:100 scribe:pen new. newBox show. ↑ newBox class message and methods shape|| scribe penup;goto:loc;turnTo:tilt;pendn. 4 timesRepeat: [scribe go:size;turn:90] Instance message and methods
Topics • History and Motivation • Structural Organization • Classes and Subclasses • Message Sending • Implementation • Object-Oriented extensions • Evaluation and Epilog
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. dispayObject box pen window
Classes and Subclasses class name box superclass displayObject Instance variable name Loc tilt size scribe Instance message and methods
Classes and Subclasses • A Superclass can be subclass of another class. Object number … dispayObject … box pen window
Classes and Subclasses • Subclasses can change the inherent method, this process is called Overriding class name complex print || ↑ realPt print + “+” imagePt print + “i” Instance variable name realPtimagePt Instance message and methods
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. Object number inventoryItem dispayObject pen bumper roof box door
An Important Issue Object number inventoryItem dispayObject brake engine bumper roof door
A simple solution ! Object inventoryItem … number dispayObject pen bumper roof box door 28
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: N.American S.American African Pets Beasts of burden Source of food Pests
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. A B C
Topics • History and Motivation • Structural Organization • Classes and Subclasses • Message Sending • Implementation • Object-Oriented extensions • Evaluation and Epilog
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.
Forms of Message Template (Argument Passing) • Message sending syntaxes: • Object_namemethodname • Object_namemethodname : argument • Object_namemethodname : 1starg 2nd arg_name:2ndarg_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.
The Smalltalk main loop is written in Smalltalk • Smalltalk is in a loop: read a command, execute the command, print the result and loop true whileTrue: [Display put: user run] run || Keyboard read eval print class name userTask Instance message and methods
Concurrency is easy to implement • 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. run || sched map: [: Task | Task run] class name scheduler Instance message and methods
Topics • History and Motivation • Structural Organization • Classes and Subclasses • Message Sending • Implementation • Object-Oriented extensions • Evaluation and Epilog
Implementation: Classes and objects • Most of Smalltalk system is written in smalltalk • Compiler, decompiler, debugger, editors,… • 97% • most of implementation data structures are smalltalk objects • Smalltalk-80 VM:not portable • 6~12KB assembly code • One man-year to produce a fully debuged version
Implementation • Smalltalk Virtual machine: • Storage manager • Encapsulate the representation of objects and organization of memory • Fetch the class of an object • Fetch and store the fields of objects • Create new objects • Interpreter • Primitive subroutines
Implementation • Smalltalk Virtual machine: • Storage manager • Interpreter • Manager for methods • Primitive subroutines • Collection of methods • For performance reasons are implemented in machine code • basic I/O functions, integer arithmetic, basic screen graphics functions
Implementation • There are three central ideas in Smalltalk • Objects • Classes • Message sending
Object representation • Representation 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 methods
Class representation • everything in Smalltalk is an object • Even classes! • Classes are instances of the class named class • Instances variables of a class object contain pointers to objects representing the information that is the same for all instances of the class • What is this information?
Representation of class object class “box” DisplayObject “loc tilt size scribe” Message dict Message dict
Representation of class object(continued) • inst. Size number of instance variables • Needed by storage manager when it instantiates an object • Message dictionaries • A method identified by the keywords • Scribe go:100 go: identifies the method • Spinner 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
Message dictionary • How should method be represented? • Too much slow if the interpreter had to decode the source form every time the method was executed • Compile the code into some form of pseudo code that can be rapidly interpreted • The source form is needed for editing and displaying class definition • Message dictionaries contain two entries for each message template
Example of message dictionary Message dictionary “grow: amount || self release. Size<- size+amount. Self show” method Push ‘slef’ Send ‘erase’,0 Pop Push ‘size’ Push ‘size’ Push ‘amount’ Send ‘+’,1 Send ‘<-’,1 pop “grow:”
Implementation of message sending • There is a strong resemblance between message passing in Smalltalk and procedure calls in other languages • Activation records: primary vehicle for procedure implementation • The same is the case in Smalltalk • Activation records hold all of the information relevant to one activation of a method
Structure of an activation record • Environment part • The context to be used for execution of the method • Instruction part • The instruction to be executed when this method is resumed • Sender part • The activation record of the method that sent the message invoking this method
Instruction part • Must designate a particular instruction in a particular method • Methods are themselves objects instances of class method • Two coordinate system is used for identifying instructions: • An object pointer defines the method-object • A relative offset identifies the particular instruction within the method object