11/10/15 251$map$check Language$design$principles • Limits*of*computability • Common*threads*in*Lisp,*ML,*Smalltalk,*Java? • Function6oriented*building*blocks*(in*Racket) • Smalltalk: • Consider:*implementation*(GC),*immutability,*metaprogramming • Surprising*features*of*language* definition? • Types,*patterns,*and*abstraction*(in*Standard*ML) • Consider:*software*engineering*implications • Principles*we*have*[not]*seen*before? • Evaluation* choices*and*orders • Steele's*plan*for*Java: • Consider:*implementing*environments,*interpreters,*delay • Issues*with*small/large* languages? • Object6oriented* building*blocks*(in*Smalltalk,* Java,*Scala) • Consider:*implementing*objects • Design*trade6offs:*composition/classification,* abstraction/extensibility • Parallelism* and*concurrency • Synthesis: unified*models,*problem6solving*as*language*design OO$essence: Object9oriented$ • Program*design*principles: • Objects*model*state/behavior*of*real6world*entities/concepts programming • Organization*by*classification*and*encapsulation • Reuse*via*extensibility and*key*language* semantics*to*support*it • Key*semantics: • Late*binding*/*dynamic*dispatch • Substitutivity and*subtyping • Inheritance*or*delegation Will*contrast*function6oriented* principles/semantics* later . 1
11/10/15 R oo ts: modeling$ the$world Smalltalk Simula 67 • Goals: • Ole6Johan*Dahl,*Kristen*Nygaard,*Norwegian*Computing*Center • Historical*context • Simulating*social*and*industrial*systems • Core*OO*ideas*from*a*different*perspective • Objects,*classes,*inheritance,*and*subtyping • Pure*OO*– influenced*many*languages Smalltalk • Non6goal:*remember*the*syntax,*program*in*it • 1970s,*Alan*Kay,*Adele*Goldberg,*Dan*Ingalls,*Xerox*PARC • Integrated*programming*system*/*interface,*personal*compute rs • Everything*is*an*object,*communicating*by*messages*(methods) CLU: • 1970s,*Barbara*Liskov,*MIT • Encapsulation*and*ADT s,*ML6like*object*type*system,*... ... By*contrast: Dynabook PDP611*" minicomputers "* c.a.$ early* 1970s "A*Personal*Computer*for*Children*of*All*Ages" Alan*Kay,*1972*(!!!) http://simh.trailing6edge.com/ http://www.pc world. com/artic le/249 951/if_i t_aint_b roke _ dont_fix_it_ancient_c ompu ters_in _use_toda y.html? pag e = 2 2
11/10/15 Xerox*Alto*running* Smalltalk$example:$Point$class Smalltalk6based*interface. • Class*definition*written*in*tabular*form class%name%%%%%%%%%%%%% Point super%class%%%%%%%%%%%%% Object class%vars%%%%%%%%%%%%%%%%%pi Encapsulation: instance%vars%%%%%%%%%%%% x%%%y Accessible*only*from* class%messages%and%methods Point*methods 〈 …names%and%code%for%methods... 〉 constructors instance%messages%and%methods 〈 …names%and%code%for%methods... 〉 Smalltalk* figures* /*code* from*Steve* Freund Instance$Messages$and$Methods Instance$Messages$and$Methods Instance*methods Instance*methods Usage Usage moveDx: dx Dy: dy | | moveDx: dx Dy: dy | | pt moveDx: 1 Dy: 1 pt moveDx: 1 Dy: 1 x <- dx + x x <- dx + x y <- dy + y y <- dy + y x: xcoord y: ycoord | | x <- xcoord pt x:3 y:2 In*Java: y <- ycoord void moveDxDy(int dx, pt.moveDxDy(1,1); int dy) { void xy(int xcoord, x = x + dx; int ycoord) { y = y + dy; x = xcoord; pt.xy(3,2); y = ycoord; } } 3
11/10/15 Instance$Messages$and$Methods Class$Messages$and$Methods Instance*methods Class*methods Examples Examples moveDx: dx Dy: dy | | pt moveDx: 1 Dy: 1 newX: xval Y: yval | | x <- dx + x p <- Point newX:3 Y:2 ^ self new x: xval y: yval y <- dy + y ( new is method inherited x: xcoord y: ycoord | | from Object) pt x:3 y:2 x <- xcoord y <- ycoord class Point { Read static Point newXY(int xval, p = Point.newXY(3,2); x | | ^x instance*variable int yval) { z <- pt x + pt y y | | ^y Point temp = new Point(); draw | | temp.xy(xval, yval); 〈 ...draw point... 〉 return temp; } } Class$Metadata and$Object$Representation Class$Messages$and$Methods (super%class) Class*methods Examples Point%object Point%class T emplate Method%dictionary x newX: xval Y: yval | | p <- Point newX:3 Y:2 newX:Y: y 2 ^ self new x: xval y: yval ... draw 3 moveDx:Dy: newOrigin | | p <- Point newOrigin ... ^ self new x: 0 y: 0 Three*primary*operations class Point { • object*creation p = Point.newOrigin(); static Point newOrigin() { • method*lookup*(dispatch) Point temp = new Point(); • field*lookup temp.xy(0, 0); return temp; } } 4
11/10/15 Dynamic$dispatch$(preview) Inheritance • ML,*Racket:*lexical*scope! Implementation/ technique: Reuse*representation* and*behavior* of*Point • (moveDxDy pt 1 1) What*function*is*called?*Does* not depend*on*pt,*1,*or*1. to*build*a*related* (and*more*specific)*ColorPoint. • Smalltalk:*"send*message"*to*object class%name%%%%%%%%%%%%% ColorPoint • pt moveDx: 1 Dy: 1 super%class%%%%%%%%%%%%% Point Method*called*depends*on pt. class%var%%%%%%%%%%%%%%%%% new%instance%%variable (super%class) instance%var%%%%%%%%%%%% color Point%object Point%class T emplate class%messages%and%methods Method%dictionary x new%method newX:xv% Y:yv% C:cv 〈 …%code%…% 〉 newX:Y: y 2 instance%messages%and%methods ... draw 3 color |%|%%^color moveDx:Dy: override%Point%method ... draw 〈 …%code%…% 〉 ColorPoint$Methods Point$/$ColorPoint representation Instance*Methods Point%class T emplate Point%object Method%dictionary x: xcoord y: ycoord c: col | | x x <- xcoord newX:Y: y 2 y <- ycoord ... draw 3 color <- c moveDx:Dy: Class*Methods ... color | | ^color newX: xv Y: yv C: cv || ColorPoint%class T emplate draw | | ... ColorPoint%object ^self new x:xv y:yv c:cv Method%dictionary x newX:Y:C: newOrigin || y 4 ^self newX:0 Y:0 C:red color color 5 draw red ... 5
11/10/15 Substitutivity Subtyping$and$Substitutivity class Rectangle { private int x,y,w,h; pt <- ??? void moveTo(int x, int y); Smalltalk pt moveDx: 1 Dy: 1 void setSize(int width, int height); void show(); pt draw void hide(); Point? } ColorPoint? class FilledRectangle { Something$else? private int x,y,w,h; Does$it$matter? private Color c; void moveTo(int x, int y); pt = ???; void setSize(int width, int height); Java pt.moveDxDy(1,1); void show(); void hide(); pt.draw(); void setFillColor(Color color); Color getFillColor(); } Subtyping$and$Substitutivity Collection$Hierarchy void f() { void f() { isEmpty,=size,= Rectangle r = Rectangle r = Collection includes:= ,=… new Rectangle(); new FilledRectangle(); at:= r.moveTo(100,100); r.moveTo(100,100); Indexed r.hide(); r.hide(); add:= Set } } at:Put: remove:= ? Updatable void g() { void g() { Dictionary FilledRectangle r = FilledRectangle r = SortedCollection new Rectangle(); new FilledRectangle(); contains: Array sort r.moveTo(100,100); r.moveTo(100,100); replaceFrom:to:With: r.setFillColor(Color.red); r.setFillColor(Color.red); Inheritance r.hide(); r.hide(); } } Subtyping 6
11/10/15 Key$things$to$revisit$more$precisely • Encapsulation • Dynamic*dispatch*(method*lookup) • Inheritance • Subtyping*and*substitutivity • How*all*of*these*interact 7
Recommend
More recommend