Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
with Personalities
Luis Blando
lblando@gte.com
Copyright 1998
Outline
Modeling behavior
Personalities
Dynamic Personalities
Synergy with Frameworks
Personalities/J language
Q&A
2
What if in here
I mess with
rtt_info?
dgsendrecv()
Excerpts extracted from: UNIX Network Programming, Richard Stevens, 1990, pp.406-414
This used to
be rtt_struct
RTT
float rtt_rtt;
float rtt_srtt;
float rtt_rttdev;
short rtt_nrexmt;
short rtt_currto;
short rtt_nxtrto;
int rtt_d_flag;
+
+
+
+
+
+
+
rtt_init()
rtt_newpack()
rtt_start()
rtt_stop()
rtt_timeout()
setX()
getX()
ReliableDGRAM
int rttfirst;
int tout_flag;
RTT myrtt;
+ sendrecv(){
myrtt.setX();
myrtt.init();
...
}
Weve encapsulated
data and functions
together. The users
keep an instance of
both
when
(one-domain, accessors)
When in doubt
Follow the data!
(anonymous :)
CRC, use-cases
The 3 amigos
intrinsic behavior.
Harrison & Ossher, SubjectOriented Programming,
1993
Wait a minute!
I know, lets just
Whats normal for me
concentrate on the basic
might not be so
behavior only for now...
for Grady or James!
8
Lion
Animal
Oviparous
Veterinary
Trainer
Feeder
ZooSys
10
11
Popular Functions
The popular functions in ZooSys
Fly() popular function
Fly
Takeoff
Ascend
ThereYet
FlapTowards
Descend
Land
// (x,y)
is the target landing spot
JumpInTheWater
void Fly(intSubmerge
x, int y, int altitude) {
MoveFin
Swim
resetMetersFlown();
Rise
Encapsulate
Takeoff();
JumpOutOfTheWater
this sequence.
Prepare
for (int a=0; a < altitude; a++) Ascend();
MoveFoot
Always the
Walk
while( !ThereYet(x, y) ) FlapTowards(x,
y);
Stabilize
same for any
for(int a=altitude; a > 0; a--) Descend();
AtEase
class...
Land();
CheckDistance
}
SprintTo
LiftOff
Land
Jump
12
Swim()
Fly()
Walk()
13
Multiple inheritance
Ambiguous/not understood
Not supported by some languages
Roles are unconstrained (I.e. are full-fledged
classes)
14
Personalities - Outline
15
The show shall start with the pink pelicans and the Af
flying across the stage. They are to land at one end of
then walk towards a small door on the side. At the sam
killer whale should swim in circles and jump just as th
by. After the jump, the sea lion should swim past the w
Eureka!
outofSome
the Remember
pool,
and this
walk
requirements
towards thestatement?
center
stage whe
OOA/D
methodologies
are
Maybe Flier is
announcer
is
waiting
for
him.
conducive to the discovery
ofofroles
a role of a lot
Wirfs-Brock et. al. animals here?
Personality - Concept
17
Personality - Architecture
Users of the object only
deal with the Personalitys
Upstream Interface
r)
e
n
i
a
(Tr
r
e
i
l
(F
n
a
c
i
(Pel
18
Personality - Components
Upstream interface
popular functions go here
Downstream interface
functions to be implemented by personifying class
Private functions
no visibility either upstream or downstream
Role-specific attributes
to keep the roles state
Constructor
to initialize the role-specific attributes
19
tic
c
a
t
Sy n
r
a
g
u
S
Constructor
Private aux.
functions and
attributes.
20
Declare intent...
Implementation
of the Bat class.
Nothing to do
with the Flier
personality here...
No default implementation
only abstract functions in DI
Basic types
Karl
ed
t
o
m
e
Behavioral buffer ate, we d
to
b
e
n
d
o
i
e
t
users of
m communicate
the
only
dathrough UI
n
soobject
e
r
m
e
t
m
f
A
ut
eco
b
r
(
d
o
e
t
t
ra
s.
Fixed
behavior
one
g
e
i
e
t
t
i
l
n
i
a
this popular
on
re functions) ispefinal
s
o
r
m
r
implementation
o
of
popular
allow f y deployable
sil
a
e
Implementation
separation
s
s
le
Luis
22
e
k
a
m
String
Today();
nt
automatically
e
since we can use personality
m
in
many
m
co
he
t
l
l
n
a
i
m
his s ifference
different domains!
T
VS.
e d
h
t
l
l
a
Expected semantics rof
! must be clear
DI
d
l
o
w
// compute and return todays date
so
the behavior
of the Cant
personalitys
check
// that
in the
format YYYYMMDD
popular
guaranteed
automatically!
String functions
Today();is somewhat
23
Clients
call UI
methods
// ofonly
Swimmer
personality
Design
reuse at the personality
void SeaWorldShow(Swimmer
s) level
{
the popular
functions
are
totally
s.Swim(10,10);
ok,
UI defined
used
Easy//
to check
automatically.
s.Submerge();
//
error,
Semantic
guarantees
(sort
of)
DI used
25
t
en
i
l
c
e
all th lity now?
a
n
o
s
r
pe
26
27
Dynamic Personalities
Why bother?
Workarounds for object migration
But wait!
Whats wrong here???
MedSys {
addPatient(Person p) {}
doSomething() {
if (p.age() > 80)
}
}
30
Delegation
Shortcomings
of static
class Person
personifies Employee, Manager
Personalities
need tohere,
know at class-creation
If However,
you forgetyou
something
time,
all to
therecompile
personalities
you might need in
youll
have
your class
andthe
refresh
futurethe instances later!
tough to do
32
Indecisive Personalities - 1
34
Indecisive Personalities -2
Report Card
Hey!
Preservation of Typing properties
Five out
Common interface among personalities
of Six isnt
Reasonable performance
that
bad!
Ubiquitous personification
same framework as static personalities
35
Ubiquitous personification
no need to declare the personalities at
class-creation time
any personality can (potentially) be
attached to any class
must redesign the method dispatch system
in the runtime environment
36
((Flier)toto).Fly(10,10,10);
i.e. class
Pelican does not know that a Fly() request will
come its way in the future!
In the clients code, we still want to be able to do typing based
// class Pelican
definition
- FDP
- AFTER
on the personalitys
upstream
interface
class Pelican implements Personable {
Mimic method dispatch
by lifting all
void
CATCHALL(Personality
p,
String
...) {...}
functions
as name,
parameters
to a meta
// client
code -function
FDP - AFTER
dispatch
Pelican toto = new Pelican();
need to add new
moreFlier(),
plumbing to ...);
make it work
toto.personify(
((Personable)toto).
CATCHALL(toto.getPers(Flier),Fly,10,10,10);
37
Pelican.Takeoff()
Pelican.Descend()
Pelican.Ascend()
Pelican.Land()
...
Class Conformance to DI
Pelican
toto =atnew
Pelican();
Must be
checked
attachment-time
// Pelican.myTakeoff, Pelican.myAscend
Implicit// but the rest share the same signature
Explicit
Nice. Though
Preservation
of Typing properties
performance
might
become
Common
interface among
personalities
an issue.
Reasonable performance
(hmmm)
same framework as static personalities
Ubiquitous personification
39
Personalities/J - Outline
40
41
void main() {
// ... taken from Zoo.pj [static] in Appendix-A
// ... knowing client sets up its data structs.
Vector all_swimmers = new Vector();
}
// ... this unknowing client knows only about
// ... the personality it cares about.
static void PoolShow(Swimmer swimmer) {
System.out.println(" PoolShow with " + swimmer);
swimmer.Swim( 1, 1 ); // and uses its UI
}
42
interface Flier
{ ... }
Flier$Ego.java
class Flier$Ego
{ ... }
Bat.pj
class Bat
personifies Flier
{ ... }
Zoo.pj
class Zoo
{ ... // uses }
Bat.java
class Bat
implements Flier
{ ... }
Zoo.java
class Zoo
{ ... // uses }
43
Static PJ - class.java
y
t
i
l
{ $swimmer.Swim(this, miles, depth);
}
ct
na
o
e
j
s
r
b
e
o
e
h
// ============== for Walkerp
t
l
l
a
can c r the DI
Walker$Ego $walker = new Walker$Ego();
fo
k
c
n!
a
o
public void Walk(int distance)
i
b
t
a
t
en
m
e
l
{ $walker.Walk(this, distance);
}
p
im
// ============== for Jumper
Jumper$Ego $jumper = new Jumper$Ego();
public void Jump(int x, int y, int alt)
{ $jumper.Jump(this, x, y, alt); }
44
Static PJ - personality.java
personality becomes interface
// Swimmer.java [static]
interface Swimmer
{
public void Swim(int miles, int depth);
void JumpInTheWater();
void Submerge();
Got rid of the UI
void MoveFin();
implementation.
void Rise();
void JumpOutOfTheWater();
}
and also all the di
keywords
45
Static PJ - pers$Ego.javaface as
er
t
n
i
e
alls
e th
c
he
s
t
t
U
i
f
o
w
o Swimmer$Ego.java [static]
h//
pe
y
s
i
t
s
e
i
th
Th
.
e
r
public
class
Swimmer$Ego
h
e
t
t
e
o
t
param
back f{ying class
ni public void Swim(Swimmer host,
o
s
r
e
p
int miles, int depth) {
host.JumpInTheWater();
for (int d = 0; d < depth; d++)
host.Submerge();
while ((miles--) > 0) host.MoveFin();
for(int d = depth; d > 0; d--)
host.Rise();
host.JumpOutOfTheWater();
}
public Swimmer$Ego()
{ }
}
46
47
forget( <personality> )
personifies( <personality> )
personalities()
48
i
if ( animal.personifies( "Swimmer"
)ng
w
on
o
m
n
k
m
o
c
((Swimmer).animal).Swim(10,10);
e
h
t
uses) ) to decide y
if ( animal.personifies( "Walker"
r
ol
e
c
v
((Walker).animal).Walk(10,10);
o
t
t
o
o
(n
pr
if ( animal.personifies( "Flier" ) ) to do
)
:
t
!
a
h
w
o
((Flier).animal).Fly(10,10,10);
w
n
k
I
if ( animal.personifies( "Jumper"
se)fu)l,
u
((Jumper).animal).Jump(10,10);
}
49
Application A
Application B
e
l
p
i
t
l
Mu
ce
n
a
t
i
Inher
A class that
already
inherits once
within its
own app.
51
+void fwork(){
hsi.fwork();}
error prone
can get out of
OID:sync
HotspotB@0x0ffe54
AppClassA
-appAttrA:int
+void fwork(){...}
+void appFn(){...}
Object identity
AppClassB
-appAttrB:int
+void fwork(){...}
+void appFn(){...}
((AppClassA)hs).appFn(); // ok
((AppClassB)hs).appFn(); // error!
// invalid cast
52
Flexible
multiple inheritance-like
Robust
solve the object identity problem introduced by
delegation
53
54
ShowFramework
ShowFramework
ShowDirector
Flier
personality Flier {
void Fly(int a) {
Takeoff();
Flap(a);
Land(); }
di void Takeoff();
di void Flap(int);
di void Land();
}
class ShowDirector {
Vector fliers=new Vector();
...
void do_show(int a) {
for(int i=0; i<fliers.size(); i++) {
Flier aFlier=
(Flier)fliers.elementAt(i);
aFlier.Fly(a);
}
}
}
TakeoffAndLandFramework
TakeoffAndLandFramework
MechFlier
LandGear
personality LandGear {
void down() {
if (_state = UP) {
servoOn();
servoMove();
servoOff();
}
}
void up() { ... }
...
}
servoOn(), servoMove()
servoOff() is LandGears
downstream interface.
56
PlaneApp
PlaneApp
Plane
B747
Wheels
WaterPlan
e
BWheel WWheel
57
rk
o
w
e
Composition using Personalities
am
r
f
e
th
here
Show_TakeoffAndLandFramework
Show Framework
ShowDirector
Flier
<<personifies>>
TakeoffAndLandFramework
Once ntiated, t r
a
ie
l
t
F
s
l
n
i
a
i
t
is
ar
P
a
l {
l
e personifies
class MechFlier
Flier
a
b
c
l
l
s
i
w lg = MechFact.makeLandGear();
hi
LandGear
T
.
r
e
t
d
c
l
...
e
o
j
h
ob
voidtakeoff()
{ place
a
ns
t
a
s
...
e
u
j
m
is
er
lg.up();
v
e
t
ve
a
e
h
i
}
w
r
t
r
e
fo { d to r
void land()
e
s
lg.down();
u
f.
e
e
r
t
... ar
jec
b
o
}
t
haFlier
// --- t
for
}
PartialFlier pf = getFromApp();
void Takeoff() { takeoff(); }
void Land()
{ land();
}
void Flap(int a) { pf.PartialFlap(a); }
LandGear
personality PartialFlier {
void PartialFlap(int a) { Flap(a);}
di void Flap(int a);
}
58
PlaneApp with
Show_TakeoffAndLandFramework
Show_TakeoffAndLandFramework
do_show()
Partial
Flier
Lots of
flexibility!
Flier
Plane
B747
WaterPlan
e
LandGear
Wheels
BWheel WWheel
However,
its intrusive since it requires
PlaneApp
59
ts
n
e
m
e
pl
m
i
n
tio
a
ing
c
i
t
l
a
p
g
p
e
a
del
ShowFramework
The
y
b
s Flier {
k
I
r
o
D
class
Plane
personifies
w
s
do_show()
rame iers
Flier TAL...
F
void Takeoff()
Fl {}
e
h
c
h
t
e
void
Land() {}
(M
to
t
n
i
)) {}
o
Flier
(a)
p void Flap(int
d
n
y
r
a
l
t
TakeoffAndLand
} and
en
)
(
Framework keoff class B747 extends Plane {
MechFlier flier = ...;
ta
takeoff()
land()
LandGear
Plane
B747
Wheels
WaterPlan
e
BWheel WWheel
PlaneApp
}
class WaterPlane extends Plane {
...
void Takeoff() { ... }
void Flap(int a) { ... }
void Land() { ... }
}
class Wheels { ... }
class BWheel extends Wheels,
personifies LandGear {
...
void servoOn() { ... }
void servoMove() { ... }
void servoOff() { ... }
}
class WWheel extends Wheels {...}
60
Other Northeastern
Collaboration-Based Work
Miras APPCs
one APPC per DI method
multiple personification becomes essential
Johans CGVs
behavior methods similar to UI methods
map methods similar to DI methods
61
class SpaceShuttle {
void EngageRockets() { ... }
void ExitAtmosphere() { ... }
void Orbit(int x, int y) { ... }
void EnterAtmosphere() { ... }
void Land() { ... }
boolean ThereYet(int x, int y) { ... }
}
!!!
:-)
...
public boolean CheckStockHas(double kilograms) {
// behavior is implemented in metric system
}
public boolean ValidateProdQuant(double pounds) {
return CheckStockHas( pounds * 0.454 );
}
63
Inheritance of Personalities
Implement some of
Fliers DI methods...
DI
64
Related Works
Using delegation
LaLonde86, Gottlob96
roles are tightly bound to the class, not easy to apply them to
different hierarchies
Relaxing inheritance
Bracha90,92, Holland93, Lucas94, Flatt98, Mezini98, Seiter98
do not attempt to commit to a specific behavior (as in popular
functions)
65
Subject-Oriented programming
Harrison93
similar goals to personalities, no concept of UI/DI, no enforcement of
LoP
Adaptive programming
Lieberherr96
no concept of UI/DI, no enforcement of LoP
Rapide
Luckham95
provided and required similar to upstream and downstream, different
scope
66
Personalities/J Compiler
Version 0.1.9
static personalities
Lets try it!
indecisive are around the corner! :-)
67