Object-Oriented Programming
Total Page:16
File Type:pdf, Size:1020Kb
2/28/2008 Why Study Smalltalk CSE 3302 Programming Languages • Purest OO language, encourage OO programming • Can inspect and change objects and the runtime system itself at run time Object-Oriented • Pioneered in many things – GhilGraphical user inter face (WidM(Window, Menu, M)Mouse) Programming – Personal workstation – Push OO into success • I invented the term Object-Oriented, and I can tell you I Chengkai Li did not have C++ in mind. -- Alan Kay Spring 2008 • Has an active community • Could have deserved more popularity Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 1 CSE3302 Programming Languages, UT-Arlington 2 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 Key Features History • Very simple syntax • 1967: Inspired by Simula67, the first OO language • Everything is object • 1971: Started by Dynabook project (Alan Kay) 3, true, nil – Hardware: GUI, pointing device, external storage, etc. Class is object • Later led to Alto (“laptop”) no control: if, loop are objects – Software: for Children • Dyyyypnamically typed: • Became Smallta lk – Variable has no type. – The class hierarchy is the type system. – Education • The language is together with its interactive runtime system – Runtime written in the language itself Is Dynabook realized? Kay doesn’t think so: – Can change the system on-the-fly – Squeak – Debug system state (image), object, class hierarchy – One Laptop Per Child project (Nicholas Negroponte, Alan Kay, …) • All data is private (protected), all methods are public. http://www.xogiving.org/ • No manual memory management and pointers Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 3 CSE3302 Programming Languages, UT-Arlington 4 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 History (cont.) Smalltalk Today • 1973: Xerox Alto Computer – First Smalltalk environment • Squeak – The ALTO Computer 1974 - Video – Seaside: a dynamic web application development framework • http://www.maniacworld.com/alto-computer- video.html – Croquet: an open source platform for collaborative 3D multi-user online applications. • 1972: Smalltalk-72 – Sophie: a digital media assembly tool to create multimedia • 1976: Smalltalk-76 documents. • 1980: Smalltalk-80 – Scratch: a toolkit for children to write games, animated • Major contributors of Smalltalk: stories. Alan Kay (2003 Turing Award), Adele Goldberg, Daniel Ingalls, • Cincom VisualWorks, IBM VisualAge, GNU Smalltalk, Dolphin, et. Al. …. Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 5 CSE3302 Programming Languages, UT-Arlington 6 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 1 2/28/2008 Resources Required Reading Squeak: Readings: • Download at http://www.squeak.org/ • Quick Tutorial: • Smalltalk by Example, Chapter 1-6 ADA Deve lopment Examp le for Squea k39k 3.9 (Chapter 7-9 are also recommended, http://squeak.preeminent.org/tut2007/html/ especially sections related to Array) • A Free Book: Squeak by Example • Section "10.6 Smalltalk" in the textook. http://www.iam.unibe.ch/~scg/SBE/ Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 7 CSE3302 Programming Languages, UT-Arlington 8 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 Smalltalk, Squeak video • http://www.youtube.com/watch?v=y_3l08tI5 wQ • http://video.google.com/videoplay?docid=-29 50949730059754521 The langgguage is tog ether with its • http://video.google.com/videoplay?docid=-90 interactive runtime system 55536763288165825 Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 9 CSE3302 Programming Languages, UT-Arlington 10 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 Smalltalk Runtime Squeak Demo • The files (.exe .changes, .img, .sources) • Runtime written in the language itself • Menu and Mouse (red-button (usually left button), yellow-button (usually right • Can change the system on-the-fly button), blue-button(Alt+red, “morphic halo”) ) • Debug system state (image), object, class • Save changes into .img and .sources hierarc hy • TitTranscript: systlltem console, log • Workspace: Run code snippet, text documents, … • do it, print it, inspect it, explore it • System Browser (Class, Object, Instance variable, Method) • Hierarchy Browser • Method Finder Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 11 CSE3302 Programming Languages, UT-Arlington 12 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 2 2/28/2008 Demo: Race Car Demo: Quinto Game http://www.youtube.com/watch?v=y_3l08tI5wQ Squeak by Example, Chapter 2 Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 13 CSE3302 Programming Languages, UT-Arlington 14 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 Demo: Quinto Game Demo: Quinto Game 1. Create new class category: SBE-Quinto 3. Add methods: initialize newCell := SBECell new. 2. Define new class: SBECell Message initialize will be Object subclass: #NameOfSubclass initialize automatically sent to the newly created instanceVariableNames: '' super initialize. object when the class has an class Var iblNiableNames: '' self label: ''. initialize method. poolDictionaries: '' self borderWidth: 2. category: ‘SBE-Quinto‘ newCell initialize. bounds := 0@0 corner: 16@16. SimpleSwitchMorph subclass: #SBECell offColor := Color paleYellow. Message: instanceVariableNames: 'mouseAction' onColor := Color paleBlue darker. classVariableNames: '' class compiled poolDictionaries: '' self useSquareCorners. category: ‘SBE-Quinto' self turnOff Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 15 CSE3302 Programming Languages, UT-Arlington 16 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 Demo: Quinto Game Demo: Quinto Game 6. Add methods: SBEGame>>initialize 4. Inspect an object initialize SBECell new (inspect it) | sampleCell width height n | self openInWorld (do it) super initialize. n := self cellsPerSide. 5. Add new class: SBEGame sampleCell := SBECell new. BorderedMorph subclass: #SBEGame width := sampleCell width. instanceVariableNames: ‘cells' height := sampleCell height. self bounds: (5@5 extent: ((width*n) @(height*n)) + (2 * classVariableNames: '' self borderWidth)). poolDictionaries: '' cells := Matrix new: n tabulate: [ :i :j | self category: 'SBE-Quinto' newCellAt: i at: j ]. 7. Put methods into category (protocol) Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 17 CSE3302 Programming Languages, UT-Arlington 18 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 3 2/28/2008 Demo: Quinto Game Demo: Quinto Game 8. Two more methods for SBECell 9. more methods for SBEGame mouseAction: aBlock For annotation only, mouseAction := aBlock cellsPerSide not the language syntax. "The number of cells along each side of the game" mouseUp: anEvent ^10 mouseAction value toggleNeighboursOfCellAt: i at: j (i > 1) ifTrue: [ (cells at: i - 1 at: j ) toggleState]. (i < self cellsPerSide) ifTrue: [ (cells at: i + 1 at: j) toggleState]. (j > 1) ifTrue: [ (cells at: i at: j - 1) toggleState]. (j < self cellsPerSide) ifTrue: [ (cells at: i at: j + 1) toggleState]. Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 19 CSE3302 Programming Languages, UT-Arlington 20 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 Demo: Quinto Game Demo: Quinto Game 9 . More SBEGame methods 9. more SBEGame methods initialize newCellAt: i at: j | sampleCell width height n | "Create a cell for position (i,j) and add it to my on--screen super initialize. representation at the appropriate screen position. Answer the n := self cellsPerSide. new cell" sampleCell := SBECell new. width := sampleCell width. height := sampleCell height. | c origin | self bounds: (5 @ 5 extent: width * n @ (height * n) + (2 * c := SBECell new. self borderWidth)). origin := self innerBounds origin. cells := Matrix new: n tabulate: [:i :j | self newCellAt: i at: self addMorph: c. j] c position: ((i -1) * c width) @ ((j -1) * c height) + origin. c mouseAction: [self toggleNeighboursOfCellAt: i at: j]. ^c Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 21 CSE3302 Programming Languages, UT-Arlington 22 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 Demo: Quinto Game Demo: MineSweeper • File Out and File In Lecture 13 – OO Programming, Lecture 13 – OO Programming, CSE3302 Programming Languages, UT-Arlington 23 CSE3302 Programming Languages, UT-Arlington 24 Spring 2008 ©Chengkai Li, 2008 Spring 2008 ©Chengkai Li, 2008 4.