Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Pharo Hands-on: 05 object model


Published on

Part of one day hands-on on Pharo

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Pharo Hands-on: 05 object model

  1. 1. Pharo Object Model Stéphane Ducasse
  2. 2. No constructors, no static methods, no operators No type declaration, no primitive types, No interfaces, no need for factory No packages/private/protected modifiers No parametrized types No boxing/unboxing Still powerful
  3. 3. A Pure OO World Only objects mouse, booleans, arrays, numbers, strings, windows, scrollbars, canvas, files, trees, compilers, sound, url, socket, fonts, text, collections, stack, shortcut, streams, … & messages
  4. 4. Simple and Uniform Everything in an object instance of a class Classes are objects Only message passing (method invocation) Only one method lookup (virtual) Packages contain classes and methods
  5. 5. Object Model Instance variables are private to the object (instance- based) ClassVariables are shared between subclasses Instance variables are protected Methods are public, virtual Single Inheritance
  6. 6. Object subclass: #Point instanceVariableNames: 'x y' classVariableNames: '' category: 'Graphics-Primitives' Single Inheritance
  7. 7. Messages + Objects
  8. 8. Object Node accept: name sendt: node1 msg The key to everything
  9. 9. Look for the message selector in the class of the receiver if found return it if not look in the superclass Execute the message on the receiver Sending a message
  10. 10. ! Point selectors ! > an IdentitySet(#eightNeighbors #+ #isZero #sortsBefore: #degrees #printOn: #sideOf: #fourNeighbors #hash #roundUpTo: #min: #min:max: #max #adaptToCollection:andSend: #quadrantOf: #crossProduct: #= #nearestPointOnLineFrom:to: #bitShiftPoint: #* #guarded #insideTriangle:with:with: #grid: #truncateTo: #y #setR:degrees: #normal Classes are objects too
  11. 11. ! Point instVarNames Classes are objects too
  12. 12. ! Point instVarNames >#('x' 'y') Classes are objects too
  13. 13. Instance creation Just messages send to special objects that are classes Morph new Character cr Rectangle origin: 0@0 corner: 100@200 Tomagashi withHunger: 10
  14. 14. Class methods are plain late bound methods as any methods!
  15. 15. Cheap factories :)
  16. 16. Cheap factories :) Parser>>parse: line self sortedParsers detect: [ :sub | (sub canParse: aLine) ifTrue: [ ^ sub newFromLine: line ] ] ! Parser>>sortedParsers ^ DocumentItem allSubclasses sorted: [ :c1 :c2 | c1 priority < c2 priority ]
  17. 17. ! ! Classes are objects too
  18. 18. ! Point class Classes are objects too
  19. 19. ! Point class >Point class ! Classes are objects too
  20. 20. ! Point class >Point class ! “Point class” is an anonymous class (called a metaclass) with only one instance: the class Point Classes are objects too
  21. 21. Class Parallel Inheritance Node class new withName: aString instance of Node name accept: aPacket send: aPacket Workstation originate: aPacket accept: aPacket aWorkstation (BigMac) Workstation class instance of
  22. 22. Lookup and Class Methods
  23. 23. About the Buttons
  24. 24. Package extensions A method can be defined in a class that is packaged in another package! Powerful to build layers
  25. 25. Defined in the Dice package Integer>>D20 ^ self D: 20 ! Integer>>D: anInteger | h | h := DiceHandle new self timesRepeat: [h addDice: (Dice faces: anInteger)]. ^ h 2 D20: two dice of 20 faces
  26. 26. Summary Everything is an object Single inheritance, public methods, protected attributes One single model Classes are simply objects too A class is instance of another class One unique method lookup, look in the class of the receiver A package can contain methods defined on classes defined in other packages.