Jump to: navigation, search

OTJ Primer/Role Playing

< OTJ Primer
Revision as of 07:32, 8 July 2010 by Stephan.cs.tu-berlin.de (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Role Playing is a new relation between classes/objects introduced by OT/J.

Class-level Binding

At class level you simply write:

public class Employee playedBy Person { /* details omitted */ }
Idea.png
Try this example
If you want to try this example, two requisites must be met: a class Person must already exist, and the declaration of Employee must be nested within a team class (see also OTJ_Primer/RoleContainment):
public team class Company { public class Employee playedBy Person {} }


The above declaration has the effect that each runtime instance of Employee will be associated to a corresponding instance of Person. The compiler statically guarantees that there will never be an Employee instance without an associated Person instance. E.g., the default constructor for a bound role like Employee requires a non-null Person argument so you can write:

Person joe = new Person("Joe");
Employee joeProgrammer = new Employee(joe);
Idea.png
Try this example
You may try the above snippet within a method of team class Company. When doing so you'll actually see a compiler warning. Can you figure out what is the issue and how the snippet can still be improved? If the warning message by itself is not clear you can always seek advice by using the "Go to Language Definition" action within the OTDT.


The syntax of OT/J does not allow direct access to the base link from a role instance to its base instance, but such access is controlled using callout method bindings, see next.

Method-level Bindings

...