Zusammenfassung
One goal of Object-Oriented Programming is to enable programmers to
craft elegant and
reusable systems. In practice however, Java programmers have no choice
but to copy and paste
code that cannot be shared via inheritance. The resulting duplication
makes systems diffcult
to understand and hard to maintain. Traits are a language feature,
originally prototyped in
Smalltalk, that directly address this barrier to reuse. Traits encapsulate
collections of methods
that can be reused anywhere in the inheritance hierarchy. We argue
that Java would benefit from
such a mechanism. To demonstrate, we present a case study of Java
Swing, a large production-
quality library, showing how we isolated pieces of duplicated code
that could not be eliminated
by conventional means and how traits could be used to eliminate them.
In addition to a lack of
multiple inheritance, our study revealed other barriers to reuse,
including the use of private, final
and synchronized qualifiers. Surprisingly, these are all easily overcome
by a natural extension
of traits.
Nutzer