70
OBJECT-ORIENTED PROGRAMMING SMALLTALK Lecturers: Hesam Setareh Salar Moarref 1 Fall 2009 – Programming languages

Lecturers: Hesam Setareh Salar Moarref 1 Fall 2009 – Programming languages

Embed Size (px)

Citation preview

  • Slide 1
  • Lecturers: Hesam Setareh Salar Moarref 1 Fall 2009 Programming languages
  • Slide 2
  • Topics History and Motivation Structural Organization Classes and Subclasses Message Sending Implementation Object-Oriented extensions Evaluation and Epilog 2
  • Slide 3
  • 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. 3
  • Slide 4
  • 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. 4
  • Slide 5
  • 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. 5
  • Slide 6
  • Topics History and Motivation Structural Organization Classes and Subclasses Message Sending Implementation Object-Oriented extensions Evaluation and Epilog 6
  • Slide 7
  • 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) Properties (Variables) Behaviors (Methods) Structural Organization
  • Slide 8
  • Scribe goto:500@500. 8 Example
  • Slide 9
  • Scribe goto:500@500. 9 Scribe go:300. Example
  • Slide 10
  • Scribe goto:500@500. 10 Scribe go:300. Scribe turn:90. Example
  • Slide 11
  • Scribe goto:500@500. 11 Scribe go:300. Scribe turn:90. Scribe go:300. Example
  • Slide 12
  • Scribe goto:500@500. 12 Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Example
  • Slide 13
  • Scribe goto:500@500. 13 Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300. Example
  • Slide 14
  • Scribe goto:500@500. 14 Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Example
  • Slide 15
  • 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. Example
  • Slide 16
  • 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 Example
  • Slide 17
  • 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] Example
  • Slide 18
  • 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@200
  • Slide 19
  • 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 namebox Instance variable nameLoc tilt size scribe Instance message and methods
  • Slide 20
  • 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 namebox Instance variable nameLoc tilt size scribe Instance message and methods class message and methods
  • Slide 21
  • Topics History and Motivation Structural Organization Classes and Subclasses Message Sending Implementation Object-Oriented extensions Evaluation and Epilog 21
  • Slide 22
  • 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 boxpenwindow
  • Slide 23
  • Classes and Subclasses 23 class namebox Instance variable name displayObject Instance message and methods superclass Loc tilt size scribe
  • Slide 24
  • Classes and Subclasses A Superclass can be subclass of another class. 24 dispayObject boxpenwindow number Object
  • Slide 25
  • print || realPt print + + imagePt print + i Classes and Subclasses 25 class namecomplex Instance variable namerealPt imagePt Instance message and methods Subclasses can change the inherent method, this process is called Overriding
  • Slide 26
  • An Important Issue In Smalltalk a class cant have several superclass. But, in the Real-World, an object can inherent from more than one object. 26 dispayObject box penbumper numberinventoryItem Object roof door
  • Slide 27
  • An Important Issue 27 dispayObject brakeenginebumper numberinventoryItem Object roof door
  • Slide 28
  • A simple solution 28 dispayObject box penbumper number inventoryItem Object roof door ! !
  • Slide 29
  • AfricanS.AmericanN.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 Pests
  • Slide 30
  • 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 A
  • Slide 31
  • Topics History and Motivation Structural Organization Classes and Subclasses Message Sending Implementation Object-Oriented extensions Evaluation and Epilog 31
  • Slide 32
  • 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. 32
  • Slide 33
  • Forms of Message Template (Argument Passing) Message sending syntaxes: Object_namemethodname Object_namemethodname : argument Object_namemethodname : 1 st arg 2 nd arg_name:2 nd 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. 33
  • Slide 34
  • run || Keyboard read eval print The Smalltalk main loop is written in Smalltalk 34 class nameuserTask 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]
  • Slide 35
  • run || sched map: [: Task | Task run] Concurrency is easy to implement 35 class namescheduler 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.
  • Slide 36
  • Topics History and Motivation Structural Organization Classes and Subclasses Message Sending Implementation Object-Oriented extensions Evaluation and Epilog 36
  • Slide 37
  • 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 37
  • Slide 38
  • 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 38
  • Slide 39
  • 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 39
  • Slide 40
  • Implementation There are three central ideas in Smalltalk Objects Classes Message sending 40
  • Slide 41
  • 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 41
  • Slide 42
  • Representation of objects Len6 c.d. loc Tilt Size scribe Len6 c.d. Loc Tilt Size Scribe Len4 c.d. X Y Box 500@600 200 500 point 42
  • Slide 43
  • 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? 43
  • Slide 44
  • Representation of class object Len8 c.d. name Super class Inst. Vars Class msgs. Inst. Msgs. Inst. Size4 class box DisplayObject loc tilt size scribe Message dict 44
  • Slide 45
  • 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 45
  • Slide 46
  • 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 46
  • Slide 47
  • Example of message dictionary. msgmethodsource. Message dictionary grow: grow: amount || self release. Size