27
Pharo Object Model Stéphane Ducasse http://www.pharo.org

Pharo Hands-on: 05 object model

  • Upload
    pharo

  • View
    75

  • Download
    3

Embed Size (px)

Citation preview

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

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

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

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

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

Object subclass: #Point

instanceVariableNames: 'x y'

classVariableNames: ''

category: 'Graphics-Primitives'

Single Inheritance

Messages + Objects

Object

Node

accept:

name

sendt:

node1

msg

The key to everything

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

!

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

!

Point instVarNames

Classes are objects too

!

Point instVarNames

>#('x' 'y')

Classes are objects too

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

Class methods are plain late bound methods as any methods!

Cheap factories :)

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 ]

!

!

Classes are objects too

!

Point class

Classes are objects too

!

Point class

>Point class

!

Classes are objects too

!

Point class

>Point class

!

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

Classes are objects too

Class Parallel Inheritance Node class

newwithName: aString

instance of

Node

nameaccept: aPacketsend: aPacket

Workstation

originate: aPacketaccept: aPacket

aWorkstation (BigMac)

Workstation

class

instance of

Lookup and Class Methods

About the Buttons

Package extensions

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

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

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.