Introduction to ObjectTeams/Java, a Role-Based Approach to Modularity With AOP
Join the DZone community and get the full member experience.Join For Free
i recently stumbled upon an interesting eclipse project called objectteams/java (ot/j), which promises improved reusability and maintenance and support for evolvable architectures by creating well-encapsulated bundles of behavior – modules – that can be applied to existing classes (via aop), when they are in the appropriate context of interaction (and not simply always, as is the case with aop). an example application is the addition of nonnull constraint to jdt via an ot/equinox plugin , without the necessity to modify jdt’s base classes. i’ve decided to write down my discoveries as the project is missing a clear and brief introduction (though it has otherwise very good documentation). this blog borrows heavily from .
disclaimer: by no means do i claim to understand ot/j completely
so what is ot/j about?
the key concepts are roles, contexts in which an entity is being used (“teams”), modules, and separation of concerns.
roles are traditionally used in domain modeling: the role customer may be played by a physical person, by a company, … . ot/j adds explicit support for roles so that behavior that is only relevant to a particular role is implemented in the class representing the role, which is than applied on the base entity class. we can see below in fig. 1 that the entity professor can play the roles supervisor for some phds and also lecturer in some courses, in this particular example prof. smith lectures oop and software engineering. the lecturing-related behavior, such as “write neatly on the blackboard” (phds are there long enough to read his natural hand writing) and handing over of slides and handouts, is implemented in the lecturer role class. thanks to this separation of concerns, as the number of roles a particular entity may play grows, the base class doesn’t get more and more bloated. roles may modify or reuse behavior of their base classes and may expose or hide any properties and methods of the base classes.
|fig. 1 (source: a precise model for contextual roles: the programming language objectteams/java .)|
contexts of interaction , called teams in the ot/j terminology, are the space where roles live, for a role is only meaningful in a particular context, such as teaching a course or participating in the phd program – you may also think of high-level use cases. teams serve basically two purposes in ot/j: first, they group related roles together (think of a package). second, they make it possible to activate or deactivate all the roles at once – e.g. when a professor and a bunch of students enter a course room then the behavior defined by the lecturer and participant roles will activate and will modify/enhance their behavior for the duration of the lesson. this ability to switch on/off modifications of behavior based on the context where a class is used is pretty nice. additionally a team may have its own set of methods and properties because it is represented by a class.
modularity and reusability are achieved by implementing independent behaviors as roles grouped into teams and applied to base classes at the run-time. ot/j offers a middle road for modularization between white-box frameworks and black-box components and makes it possible to evolve and adapt a system while avoiding lot of changes to the base classes. one of the examples i’ve seen uses an abstract team with a subscriber, which collects credit for some bonusitems. the team is then specialized with a concrete flightbonus team, which applies this behavior to the existing entity classes passenger and flight, where the “credit” being collected are bonus miles. the same abstract team could be applied to a shop customer shopping with a customer card and collecting some bonus for each purchase.
what is it good for?
ot/j makes it possible to implement systems in a more natural way by supporting explicitly the notion of role, narrowing thus the gap between the business, business analysts and the developers. at the same time it helps to remove unnecessary coupling and to structure functionality in a better, more reusable way.
the authors themselves have experienced these benefits:
at a technical level applying ot/j for the implementation of its own tools herrmann & mosconi (2007) has demonstrated significantly improved maintainability of components that re-use and adapt existing components.
how is it implemented?
ot/j extends java syntax to support natively the concepts of a team, role, and extension/modification of existing behavior of base classes. you compile it with a special ot/j compiler and it then applies load-time weaving to actually enable the application of roles to their base classes. teams and their roles are activated either implicitly when you call a method on a team class, the activation lasting for the duration of the call, or explicitly, so that a role may apply even if the client of the entity is unaware of it (for instance a flight reservation system doesn’t perhaps need to know or care that there is some support for collecting bonus miles).
an example of the syntax, showing a team and replacement of a method in the base class main (an around advice in the aop language):
role is a class, which explicitly refers to the class that it
modifies/enhances. at run-time, the modifications it introduces are
injected into the base class when it is in the appropriate context.
- methods of the base class may be modified in the around-advice style (“callin-binding”) using “arolemethod <- replace abaseclassmethod”
- methods of the base class may be exposed by the role (“callout-binding”): “adelegatingrolemethod -> abaseclassmethod”
- also properties of the base class may be exposed via getters/setters: “getarolegetter -> get abaseclassfield”
- team is a class, which may contain roles – either as nested classes or in a package-style folder/file structure – and its own methods and properties. roles have a link to their team. when a team is activated then ot/j tries to automatically apply its roles to the eligible interacting objects based on their types (the programmer may tune that with “guard predicates”).
what’s the difference between aop and ot/j?
aop deals only with code injection and usually the same code (advice) is applied to multiple methods in multiple classes. ot/j is much more than that with its explicit support for roles and teams and their context-sensitive activation. also a method in a role normally only modifies one method in one base class, so the mapping isn’t 1:n as in aop but 1:1.
ot/j also supports the traditional modification of multiple joint points but this should be avoided as much as possible because it makes it more difficult to understand the runtime behavior and to refactor the code, ot/j is on purpose more explicit.
see  for a deeper discussion.
separation of concerns is not always easy because we often need to apply the same – or very similar – behavior at several places and oop doesn’t always lend itself to extraction and reuse of the behavior. aop can help here by injecting the behavior defined at one place to all the places where it is needed but when its power is overused then it is very difficult to understand the runtime structure of a program from just looking at its source codes. ot/j makes a very good compromise here by allowing roles to modify existing classes but making the application explicit and 1:1 (further reusability is possible by defining common functionality in an abstract team and binding it to different set of entities via concrete sub-teams, as in the flightbonus example). by using the language of business analysis such as roles, ot/j makes it easier to understand and apply this technique to real projects.
ot/j certainly looks very interestingly and can benefit a complex project greatly provided that it is applied correctly. certainly it tends to be less obscure than low-level aop with e.g. aspectj.
ot/j ss currently in incubation due to its recent move to eclipse, but the project is alive since around 2003, so it should be pretty mature. there is also tooling supporting it, for example a set of eclipse plugins.
according to the developers, the project delivers what it promises:
during several case studies we have collected data about code sizes, about structural properties of the code as well as about the development process regarding productivity and maintainability. all these data support our approach, …
-  a precise model for contextual roles: the programming language objectteams/java . – very good explanation of ot/j and the design decisions behind it, the only downside is the length of 25 pages
-  ot blog: compare object teams to aop? – it also explains very well what playing a role means
-  object teams quick-start – a hands-on tutorial
- patterns of good design with ot/j
Opinions expressed by DZone contributors are their own.