You are on page 1of 22

Software Composition Paradigms

Sommersemester 2015
Radu Muschevici
Software Engineering Group, Department of Computer Science

2015-04-28

1 / 22

Exam Date

The exam date has changed to:

Wednesday, 15 July 2015, 19.0021.00

Rooms S101/A1 and S311/08

Note: The lecture on Tuesday, 7 July 2015 will be used for review and
questions (no new content).

2 / 22

Prototype-based
Programming

3 / 22

Self: The Movie

Watch the Self 1 programming language video:


https://www.youtube.com/watch?v=Ox5P7QyL774

http://www.selflanguage.org/
4 / 22

Prototype-based Programming Languages


Prototype: an original or rst model of something from which other
forms are copied or developed
[Merriam-Webster Dictionary]

No classes; only objects

Any object can be the prototype of another object

Object creation by copying the prototype

Objects dene their own properties and methods

Objects delegate behaviour to other objects

Many languages (but only few used outside research):


JavaScript, Self, Io, NewtonScript, Omega, Cecil, Lua, Object-Lisp,
Exemplar, Agora, ACT-I, Kevo, Moostrap, Obliq, Garnet
5 / 22

Design Goals

Simplicity

Leave out everything that dilutes the paradigm

Everything is an object

Fewer concepts simpler programming model, easier to


understand, explain and use

Simpler relationship between objects: object inherits from other


object (Compare to class-based, where 1: object is an instance of
a class, and 2: objects class is a subclass of another class)

6 / 22

The The
SelfObject
Object-Model
Example:
Model of Self
:x

paint

:y
how to paint objects

parent*
width

width:
height

height:

assignment primitives
overwrite the corresponding
get methods

parent*
height
height:

variables are modeled


as methods returning
constant values

^width

:x
width: x

Every object contains a

collection
of slotscontains
Every object

Each
slot has of
a name
collection
slots and an
object
Every
Each
slot
a nameasand an
slot
canhas
be marked
parent
slot
object
A non-method object simply
returns
Everyitself
slot when
can be
marked as
invoked
asparent
a method
slot
A method objects contains a
piece
of code thatsobject
executed
A non-method
simply
on invocation
returns itself when invoked as
Slots of method objects can
methodas argument slots
beamarked

A method object contains a


piece of code thats executed
on invocation

Slots of method objects can be


marked as argument slots
7 / 22

Some Issues with Classes


In OO programs, objects send messages to other objects. OO source
code shows usclasses inheriting from classes. Oops. There is a
complete disconnect in OOP between the source code and the
runtime entities.
[O. Nierstrasz, ECOOP 2010]

Classes are abstract (In the real world, there are only objects)

Class-based languages force software engineers to design from


abstract to concrete

More natural: from concrete examples to abstract concepts

Class-based architectures are sometimes seen as too rigid

Designing a class inheritance hierarchy not always easy

8 / 22

Classication: An easy example


Three objects o1, o2, o3
sharing properties a, b, c, d:

A corresponding class hierarchy:


C1

o1

o2

o3

a
b

a
b
c

a
b
d

a
b

C2

C3

a
b
c

a
b
d

o1 = new C1();
o2 = new C2();
o3 = new C3();

9 / 22

Family Resemblance: When classication is not easy

Philosophical idea made popular by Wittgenstein (18891951)

Observation: some things have no essential features in common,


even though they may belong to same category.

Instead they have a series of overlapping similarities.

Members of a family are difcult to classify as a hierarchy.

Examples: works of art, games,

10 / 22

Family Resemblance: Example

Three objects: o1, o2, o3


sharing some properties: a, b, c, d

o1

o2

o3

a
b
c

b
c
d

d
a

11 / 22

Family Resemblance: Classication Attempt


A

BC
b
c

C1

C2

C3

a
b
c

b
c
d

d
a
12 / 22

Family Resemblance: Delegation

o2
o1
a
b
c

b
c
d
o3
d
a

13 / 22

Prototypes & Object Creation

Prototypes

Any object can be the prototype for another object

Object Creation

By copying (cloning) an existing object (the prototype)

Some languages support object creation ex nihilo

Object is related to other objects through delegation

14 / 22

Delegation

Smalltalk
instances and classes
how to
print objects

print

Mechanism to share data and


(class)
behaviour among
objects
(name)
Object

An object (superclass)
has parent(s) nil(sometime
(inst vars)
(none)
the prototype)
(methods)

If object receives a message it cannot


handle, it delegates it to its parent(s)
...

(class)
(x)
(y)

3
5

7
9

how to
print objects

print

...

(class)
Point
(name)
Multiple parents
multiple
(superclass)
inheritance (inst
(diamond
problem
class, x, y
vars)
More exible
(methods)
than inheritance

SELF objects

parent*
+

how to
add points

etc.)

how to
add points
parent*
x
x:
y
y:

3
!
5
!

parent*
x
x:
y
y:

7
!
9
!
15 / 22

Object Creation: Flexibility

Objects built from same prototype can differ in their behaviour

Objects can dynamically change state variables and behaviour


rectangle
width
height
getArea() {width*height}

clone
+ remove height
+ change getArea() code

clone
+ add depth
+ remove getArea()
+ add getVolume()
3dBox

square
width
getArea() {width*width}

width
height
depth
getVolume() {width*height*depth}
16 / 22

Delegation: Flexibility (2)

An objects parent(s) can change dynamically

parent*

...

parent*

...

width

width

30

height

10

height

depth

12

parent*

17 / 22

Delegation: Flexibility (2)

An objects parent(s) can change dynamically

parent*

...

parent*

...

width

width

30

height

10

height

depth

12

parent*

18 / 22

Delegation: Flexibility (3)

Objects can have unique or shared values for state variables

parent*

parent*

...

width

height

10

parent*
width

height

32

19 / 22

Inheritance vs. Delegation


Inheritance

Objects of the same class have the same behaviour

Objects have unique values for state variables

Object structure and behaviour is xed at compile-time

The class of an object is xed

Delegation

Objects built from same prototype can differ in their behaviour

Objects can have unique or shared values for state variables

Objects can dynamically change (add, remove) operations and


state variables

Objects parent(s) change can change dynamically

Delegation is more exible than inheritance. Is this added exibility


helpful or harmful?
20 / 22

Class-based vs. Prototype-based Languages

Class-based

Classes describe how objects behave

Good for situations where many objects have same behaviour


(one class many instances)

Extra effort to create singleton objects

Prototype-based

Objects embody description of their own behaviour

Good for situations where many objects have unique behaviour

Singleton objects are easy

21 / 22

This Weeks Reading Assignment

David Ungar, Craig Chambers, Bay-Wei Chang, and Urs Hlzle,


Organizing Programs Without Classes, Lisp and Symbolic
Computation 4(3), Kluwer Academic Publishers, June, 1991.

Download link:
http://bibliography.selflanguage.org/
organizing-programs.html

22 / 22

You might also like