1 / 70

OBJECT-ORIENTED PROGRAMMING SMALLTALK

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.

tress
Télécharger la présentation

OBJECT-ORIENTED PROGRAMMING SMALLTALK

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OBJECT-ORIENTED PROGRAMMINGSMALLTALK Lecturers: HesamSetareh SalarMoarref Fall 2009 – Programming languages

  2. Topics • History and Motivation • Structural Organization • Classes and Subclasses • Message Sending • Implementation • Object-Oriented extensions • Evaluation and Epilog

  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.

  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.

  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.

  6. Topics • History and Motivation • Structural Organization • Classes and Subclasses • Message Sending • Implementation • Object-Oriented extensions • Evaluation and Epilog

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

  8. Example Scribe goto:500@500.

  9. Example Scribe goto:500@500. Scribe go:300.

  10. Example Scribe goto:500@500. Scribe go:300. Scribe turn:90.

  11. Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300.

  12. Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90.

  13. Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300.

  14. Example Scribe goto:500@500. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90. Scribe go:300. Scribe turn:90.

  15. 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.

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. Topics • History and Motivation • Structural Organization • Classes and Subclasses • Message Sending • Implementation • Object-Oriented extensions • Evaluation and Epilog

  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. dispayObject box pen window

  23. Classes and Subclasses class name box superclass displayObject Instance variable name Loc tilt size scribe Instance message and methods

  24. Classes and Subclasses • A Superclass can be subclass of another class. Object number … dispayObject … box pen window

  25. 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

  26. 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

  27. An Important Issue Object number inventoryItem dispayObject brake engine bumper roof door

  28. A simple solution ! Object inventoryItem … number dispayObject pen bumper roof box door 28

  29. 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

  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. A B C

  31. Topics • History and Motivation • Structural Organization • Classes and Subclasses • Message Sending • Implementation • Object-Oriented extensions • Evaluation and Epilog

  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.

  33. 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.

  34. 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

  35. 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

  36. Topics • History and Motivation • Structural Organization • Classes and Subclasses • Message Sending • Implementation • Object-Oriented extensions • Evaluation and Epilog

  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

  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

  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

  40. Implementation • There are three central ideas in Smalltalk • Objects • Classes • Message sending

  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

  42. Representation of objects

  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?

  44. Representation of class object class “box” DisplayObject “loc tilt size scribe” Message dict Message dict

  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

  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

  47. 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:”

  48. 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

  49. 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

  50. 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

More Related