OBJECT-ORIENTED PROGRAMMING SMALLTALK

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

description

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. - PowerPoint PPT Presentation

Transcript of OBJECT-ORIENTED PROGRAMMING SMALLTALK

OBJECT-ORIENTED PROGRAMMING SMALLTALK

OBJECT-ORIENTED PROGRAMMINGSMALLTALKLecturers:Hesam SetarehSalar Moarref1Fall 2009 Programming languages1TopicsHistory and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementationObject-Oriented extensionsEvaluation and Epilog

22History and MotivationAlan 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.

33History and MotivationKay 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.

4Simula based on ALGOL in the 1960s , get idea from LOGO which implement in MIT for 8-12 years old child.4The born of SmalltalkSmalltalk 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.

55TopicsHistory and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementationObject-Oriented extensionsEvaluation and Epilog

66In Smalltalk everything is object, including variables, numbers, strings, etc. For example : x*2Any object has two major parts:

7Properties (Variables)

Behaviors (Methods)Structural Organization7Scribe goto:500@500.

8Example8Scribe goto:500@500.

9Scribe go:300.

Example9Scribe goto:500@500.

10Scribe go:300.

Scribe turn:90.

Example10Scribe goto:500@500.

11Scribe go:300.

Scribe turn:90.

Scribe go:300.

Example11Scribe goto:500@500.

12Scribe go:300.

Scribe turn:90.

Scribe go:300.

Scribe turn:90.

Example12Scribe goto:500@500.

13Scribe go:300.

Scribe turn:90.

Scribe go:300.

Scribe turn:90.

Scribe go:300.

Example13Scribe goto:500@500.

14Scribe go:300.

Scribe turn:90.

Scribe go:300.

Scribe turn:90.

Scribe go:300.

Scribe turn:90.

Example14Scribe goto:500@500.

15Scribe go:300.

Scribe turn:90.

Scribe go:300.

Scribe turn:90.

Scribe go:300.

Scribe turn:90.

Scribe go:300.

Example15Scribe goto:500@500.

16Scribe go:300.

Scribe turn:90.

Scribe go:300.

Scribe turn:90.

Scribe go:300.

Scribe turn:90.

Scribe go:300.

Scribe turn:90

Example16Scribe goto:500@500.

17Scribe 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]Example17ClassA class is a plan, which objects are made from.

Getting a new object from a class is called instantiation.

18anotherScribe pen newAt:200@200Previous example : scribe from pen , building example18shape|| 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 showClass Definition19class nameboxInstance variable nameLoc tilt size scribeInstance message and methodsPrevious example : scribe from pen , building example19newAt:initialLocation|newBox| newBox self new. newBox setLoc:initialLocation tilt:0 size:100 scribe:pen new. newBox show. newBoxshape|| scribe penup;goto:loc;turnTo:tilt;pendn. 4 timesRepeat: [scribe go:size;turn:90]Class Definition20class nameboxInstance variable nameLoc tilt size scribeInstance message and methodsclass message and methodsPrevious example : scribe from pen , building example20TopicsHistory and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementationObject-Oriented extensionsEvaluation and Epilog

2121Classes and SubclassesSmalltalk 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.

22dispayObjectboxpenwindowAll of these class has goto , loc , 22Classes and Subclasses23class nameboxInstance variable namedisplayObjectInstance message and methodssuperclassLoc tilt size scribe23Classes and SubclassesA Superclass can be subclass of another class.

24dispayObjectboxpenwindownumberObjectObject is superclass of all another class. For example it has print method which call when each class printed.24print || realPt print + + imagePt print + i Classes and Subclasses25class namecomplexInstance variable namerealPt imagePtInstance message and methods Subclasses can change the inherent method, this process is called OverridingLike toString java25An Important IssueIn Smalltalk a class cant have several superclass.But, in the Real-World, an object can inherent from more than one object.

26dispayObjectboxpenbumpernumberinventoryItemObjectroofdoor26An Important Issue27dispayObjectbrakeenginebumpernumberinventoryItemObjectroofdoor27A simple solution2828dispayObjectboxpenbumpernumberinventoryItemObjectroofdoor!28African

S.American

N.American

Another aspect of that issueThe way of classification causes class grouping.It is possible for some classes to have several ways of classification.Orthogonal classification:29PetsBeasts of burdenSource of foodPests29Multiple Inheritance raises difficult problemMultiple inheritance allows a class to be an immediate subclass of more than one class.It solve one problem but, brings many new problems.

30CBA30TopicsHistory and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementationObject-Oriented extensionsEvaluation and Epilog

3131Dynamic versus Static, Strong versus weakSmalltalk uses dynamic type checking like LISP.Pascal and Ada use static type checking.Dynamic type checking does not imply weak type.3232Forms of Message Template (Argument Passing)Message sending syntaxes:Object_namemethodnameObject_namemethodname : argumentObject_namemethodname : 1st arg 2nd arg_name:2nd arg_valWe have a problem with arithmetic expression: (x+2)*y (x plus : 2) times : yIt became possible for one-parameter message to use an operation.3333run || Keyboard read eval printThe Smalltalk main loop is written in Smalltalk34class nameuserTaskInstance message and methodsSmalltalk is in a loop: read a command, execute the command, print the result and looptrue whileTrue: [Display put: user run]

34run || sched map: [: Task | Task run]Concurrency is easy to implement35class nameschedulerInstance message and methodsSched 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. 35TopicsHistory and MotivationStructural OrganizationClasses and SubclassesMessage SendingImplementationObject-Oriented extensionsEvaluation and Epilog

3636Implementation: 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 version3737ImplementationSmalltalk 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 subroutines3838ImplementationSmalltalk 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 3939Implementation There are three central ideas in SmalltalkObjectsClassesMessage sending

4040Object representationRepresentation of an object must contain just that information that varies from object to objectInstance variables The information stored with the class includes the class methods and instance methods4141Representation of objectsLen6c.d.locTiltSizescribeLen 6c.d.LocTiltSizeScribeLen4c.d.XYBox500@600200500point4242Class representation everything in Smalltalk is an objectEven 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?4343Representation of class objectLen8c.d. nameSuper classInst. VarsClass msgs.Inst. Msgs.Inst. Size4classboxDisplayObjectloc tilt size scribeMessage dictMessage dict4444Representation of class object(continued) inst. Size number of instance variables Needed by storage manager when it instantiates an objectMessage dictionariesA method identified by the keywordsScribe go:100 go: identifies the methodSpinner newAt: 500@200 rate:1 newAt:rate: is methodMessage templateFor each message template acceptable to a class or its instances, one of the message dictionaries must contain an entry 4545Message 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 template4646Example of message dictionary . . .msgmethodsource . . .Message dictionarygrow:grow: amount || self release.Size