Top Banner
33
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: Reclassification
Page 2: Reclassification

Scenario

In a first person shooter:

enemies tend to change their behavior when they become aware of theplayer’s presence (and then again when they empty their magazines),

under the hood, they are typically represented by objects and

references pointing at them are often spread across the whole memory.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 2 / 14

Page 3: Reclassification

Problem

How to represent enemies?

Single class with a typecode—clumsy.

Multiple classes, events and pointer redirection—too complicated, notfeasible.

The strategy pattern.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 3 / 14

Page 4: Reclassification

Problem

How to represent enemies?

Single class with a typecode—clumsy.

Multiple classes, events and pointer redirection—too complicated, notfeasible.

The strategy pattern.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 3 / 14

Page 5: Reclassification

Problem

How to represent enemies?

Single class with a typecode—clumsy.

Multiple classes, events and pointer redirection—too complicated, notfeasible.

The strategy pattern.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 3 / 14

Page 6: Reclassification

Reclassification

Change of a class of an object during its lifetime.

Not implemented in current industrial programming languages.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 4 / 14

Page 7: Reclassification

Issues

Implementation (flexible and efficient).

Type safety.

Halfway executed methods invoked on the reclassified object.

Overall design: cleanliness, consistency and interaction withother features of the language.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 5 / 14

Page 8: Reclassification

Issues

Implementation (flexible and efficient).

Type safety.

Halfway executed methods invoked on the reclassified object.

Overall design: cleanliness, consistency and interaction withother features of the language.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 5 / 14

Page 9: Reclassification

Issues

Implementation (flexible and efficient).

Type safety.

Halfway executed methods invoked on the reclassified object.

Overall design: cleanliness, consistency and interaction withother features of the language.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 5 / 14

Page 10: Reclassification

Issues

Implementation (flexible and efficient).

Type safety.

Halfway executed methods invoked on the reclassified object.

Overall design: cleanliness, consistency and interaction withother features of the language.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 5 / 14

Page 11: Reclassification

Issues

Implementation (flexible and efficient).

Type safety.

Halfway executed methods invoked on the reclassified object.

Overall design: cleanliness, consistency and interaction withother features of the language.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 5 / 14

Page 12: Reclassification

The Strategy Pattern

Implementation: indirect pointers.

Envelope defines its own type.

Halfway executed methods continue their execution on the originalobject—they are not aware of the replacement. However, they maydamage the context.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 6 / 14

Page 13: Reclassification

The Strategy Pattern

Implementation: indirect pointers.

Envelope defines its own type.

Halfway executed methods continue their execution on the originalobject—they are not aware of the replacement. However, they maydamage the context.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 6 / 14

Page 14: Reclassification

The Strategy Pattern

Implementation: indirect pointers.

Envelope defines its own type.

Halfway executed methods continue their execution on the originalobject—they are not aware of the replacement. However, they maydamage the context.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 6 / 14

Page 15: Reclassification

Smalltalk’s become:

Implementation: redirect pointer referencing class object.

No type safety (as in the rest of the language).

Halfway executed methods continue their execution—it is up to theprogrammer to make sure they cause no harm.

Overall design in sync with the rest of the language.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 7 / 14

Page 16: Reclassification

Smalltalk’s become:

Implementation: redirect pointer referencing class object.

No type safety (as in the rest of the language).

Halfway executed methods continue their execution—it is up to theprogrammer to make sure they cause no harm.

Overall design in sync with the rest of the language.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 7 / 14

Page 17: Reclassification

Smalltalk’s become:

Implementation: redirect pointer referencing class object.

No type safety (as in the rest of the language).

Halfway executed methods continue their execution—it is up to theprogrammer to make sure they cause no harm.

Overall design in sync with the rest of the language.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 7 / 14

Page 18: Reclassification

Smalltalk’s become:

Implementation: redirect pointer referencing class object.

No type safety (as in the rest of the language).

Halfway executed methods continue their execution—it is up to theprogrammer to make sure they cause no harm.

Overall design in sync with the rest of the language.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 7 / 14

Page 19: Reclassification

Gilgul

Implementation: indirect pointers.

Optionally, an exception is raised when reclassifying an object withmethods on the stack. When all methods invoked on that objects areunwound, the last one is restarted.

Reclassification is a global issue—indirectly or directly reclassifyingmethods should either be side-effect free or abortable/restartable.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 8 / 14

Page 20: Reclassification

Gilgul

Implementation: indirect pointers.

Optionally, an exception is raised when reclassifying an object withmethods on the stack. When all methods invoked on that objects areunwound, the last one is restarted.

Reclassification is a global issue—indirectly or directly reclassifyingmethods should either be side-effect free or abortable/restartable.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 8 / 14

Page 21: Reclassification

Gilgul

Implementation: indirect pointers.

Optionally, an exception is raised when reclassifying an object withmethods on the stack. When all methods invoked on that objects areunwound, the last one is restarted.

Reclassification is a global issue—indirectly or directly reclassifyingmethods should either be side-effect free or abortable/restartable.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 8 / 14

Page 22: Reclassification

Fickle

Three types of classes: normal, root and state.

Root classes can only inherit from normal classes.Root classes can only be parents to state classes.State classes can only inherit from root classes or state classes.

State classes can not be used as field types.

Methods have to declare root classes of directly or indirectlyreclassified objects.

Static types of variables change conservatively to accommodatefor the effects of reclassification.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 9 / 14

Page 23: Reclassification

Fickle

Three types of classes: normal, root and state.

Root classes can only inherit from normal classes.Root classes can only be parents to state classes.State classes can only inherit from root classes or state classes.

State classes can not be used as field types.

Methods have to declare root classes of directly or indirectlyreclassified objects.

Static types of variables change conservatively to accommodatefor the effects of reclassification.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 9 / 14

Page 24: Reclassification

Fickle

Three types of classes: normal, root and state.

Root classes can only inherit from normal classes.Root classes can only be parents to state classes.State classes can only inherit from root classes or state classes.

State classes can not be used as field types.

Methods have to declare root classes of directly or indirectlyreclassified objects.

Static types of variables change conservatively to accommodatefor the effects of reclassification.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 9 / 14

Page 25: Reclassification

Fickle

Three types of classes: normal, root and state.

Root classes can only inherit from normal classes.Root classes can only be parents to state classes.State classes can only inherit from root classes or state classes.

State classes can not be used as field types.

Methods have to declare root classes of directly or indirectlyreclassified objects.

Static types of variables change conservatively to accommodatefor the effects of reclassification.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 9 / 14

Page 26: Reclassification

Example

root class Player {bool brave;

abstract Weapon kissed(){Player}}

state class Frog extends Player {Vocal pouch;

Weapon kissed(){Player}{this⇓Prince; sword = new Weapon}}

state class Prince extends Player {Weapon sword;

Weapon kissed(){Player}{sword}}

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 10 / 14

Page 27: Reclassification

Example (II)

bool play(Player p, Frog f){Player} {f.pouch; // correct

p.kissed();

f.pouch; // incorrect

p.brave; //correct

}

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 11 / 14

Page 28: Reclassification

Predicate Classes

Combination of multiple dispatch with automatic reclassification.

An object is reclassified once a predicate is fulfilled.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 12 / 14

Page 29: Reclassification

Typing Issues of Predicate Classes

The system uses multimethods.

It guarantees no method invocation is ambiguous.

Compiler raises an error if it can not deduce no ambiguity can occur.

A programmer may provide an additional information which helps todispel suspicions of ambiguity using disjoint and cover primitives.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 13 / 14

Page 30: Reclassification

Typing Issues of Predicate Classes

The system uses multimethods.

It guarantees no method invocation is ambiguous.

Compiler raises an error if it can not deduce no ambiguity can occur.

A programmer may provide an additional information which helps todispel suspicions of ambiguity using disjoint and cover primitives.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 13 / 14

Page 31: Reclassification

Typing Issues of Predicate Classes

The system uses multimethods.

It guarantees no method invocation is ambiguous.

Compiler raises an error if it can not deduce no ambiguity can occur.

A programmer may provide an additional information which helps todispel suspicions of ambiguity using disjoint and cover primitives.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 13 / 14

Page 32: Reclassification

Typing Issues of Predicate Classes

The system uses multimethods.

It guarantees no method invocation is ambiguous.

Compiler raises an error if it can not deduce no ambiguity can occur.

A programmer may provide an additional information which helps todispel suspicions of ambiguity using disjoint and cover primitives.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 13 / 14

Page 33: Reclassification

See

Sophia Drossopoulou, Ferruccio Damiani, Mariangiola Dezani-Ciancaglini,and Paola Giannini. More Dynamic Object Reclassification: FickleII.ACM Transactions on Programming Languages and Systems 24, 2 (March2002). 153–191. http://doi.acm.org/10.1145/514952.514955

Craig Chambers. Predicate Classes. Proceedings of the 7th EuropeanConference on Object-Oriented Programming (ECOOP ’93), OscarNierstrasz (Ed.). Springer-Verlag, London, UK. 268–296.

Michal Pıse (CTU in Prague) Object Programming Lect. 7: Reclassification November 7, 2010 14 / 14