Top Banner
Pharo Object Model Stéphane Ducasse http://www.pharo.org
27
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Pharo Hands-on: 05 object model

Pharo Object ModelStéphane Ducasse http://www.pharo.org

Page 2: Pharo Hands-on: 05 object model

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

Page 3: Pharo Hands-on: 05 object model

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

Page 4: Pharo Hands-on: 05 object model

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

Page 5: Pharo Hands-on: 05 object model

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

Page 6: Pharo Hands-on: 05 object model

Object subclass: #Point

instanceVariableNames: 'x y'

classVariableNames: ''

category: 'Graphics-Primitives'

Single Inheritance

Page 7: Pharo Hands-on: 05 object model

Messages + Objects

Page 8: Pharo Hands-on: 05 object model

Object

Node

accept:

name

sendt:

node1

msg

The key to everything

Page 9: Pharo Hands-on: 05 object model

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

Page 10: Pharo Hands-on: 05 object model
Page 11: Pharo Hands-on: 05 object model

!

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

Page 12: Pharo Hands-on: 05 object model

!

Point instVarNames

Classes are objects too

Page 13: Pharo Hands-on: 05 object model

!

Point instVarNames

>#('x' 'y')

Classes are objects too

Page 14: Pharo Hands-on: 05 object model

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

Page 15: Pharo Hands-on: 05 object model

Class methods are plain late bound methods as any methods!

Page 16: Pharo Hands-on: 05 object model

Cheap factories :)

Page 17: Pharo Hands-on: 05 object model

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 ]

Page 18: Pharo Hands-on: 05 object model

!

!

Classes are objects too

Page 19: Pharo Hands-on: 05 object model

!

Point class

Classes are objects too

Page 20: Pharo Hands-on: 05 object model

!

Point class

>Point class

!

Classes are objects too

Page 21: Pharo Hands-on: 05 object model

!

Point class

>Point class

!

“Point class” is an anonymous class (called a metaclass) with only one instance: the class Point

Classes are objects too

Page 22: Pharo Hands-on: 05 object model

Class Parallel Inheritance Node class

newwithName: aString

instance of

Node

nameaccept: aPacketsend: aPacket

Workstation

originate: aPacketaccept: aPacket

aWorkstation (BigMac)

Workstation

class

instance of

Page 23: Pharo Hands-on: 05 object model

Lookup and Class Methods

Page 24: Pharo Hands-on: 05 object model

About the Buttons

Page 25: Pharo Hands-on: 05 object model

Package extensions

A method can be defined in a class that is packaged in another package! Powerful to build layers

Page 26: Pharo Hands-on: 05 object model

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

Page 27: Pharo Hands-on: 05 object model

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