1 / 34

Traits: Composable Units of Behaviour

Overview. Motivation: problems with typical inheritance strategiesIntroduction to TraitsTraits and inheritanceAdditional information. Motivation: Problems with Inheritance. Inheritance Overview. Inheritance is one of the core features of object-oriented languagesProvides a means to share state

rachel
Download Presentation

Traits: Composable Units of Behaviour

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Traits: Composable Units of Behaviour By: Schärli, Ducasse, Nierstrasz, and Black Presented by: Mark Hills ACM SIGPLAN, UIUC January 31, 2005

    2. Overview Motivation: problems with typical inheritance strategies Introduction to Traits Traits and inheritance Additional information

    3. Motivation: Problems with Inheritance

    4. Inheritance Overview Inheritance is one of the core features of object-oriented languages Provides a means to share state and functionality between classes Several kinds in use, including Single inheritance Interface inheritance Multiple inheritance Mixins

    5. Single Inheritance Standard type of inheritance in most languages, including Smalltalk, Java, and C# Benefit: easier to implement and reason about than most other types of inheritance Benefit: reuse of behavior and state are both straightforward

    6. Single Inheritance Drawback: does not always allow for proper modeling of the problem domain Drawback: often leads to the copying of code into multiple locations, or to moving code into places it does not logically belong

    7. Interface Inheritance Often used in conjunction with languages that provide only single inheritance, such as Java and C# Can also be used in languages such as C++ (abstract base classes with pure virtual functions) Drawback: does not allow for reuse

    8. Multiple Inheritance Available in some languages, such as C++ and Eiffel Allows a class to be inherited from anywhere in the class hierarchy Problem: classes serve two roles Unit of reuse: would like to reuse anywhere Generator of instances: need a “home” in the hierarchy Because of the latter, classes may not actually be the best way to reuse functionality (more on this later)

    9. Multiple Inheritance Several well known problems with multiple inheritance Inheritance conflicts Two classes provide a member of the same name A class appears multiple times in the inheritance hierarchy (known as “the diamond problem”, “fork-join inheritance”, or even “the diamond of death”) State: one copy, or several?

    10. Multiple Inheritance Overridden features Accessing overridden fields and methods is more difficult, can require exact knowledge of where they live Still no way to access not yet added features from a superclass (generic wrappers) -- although generics fix this in C++

    11. Example: Generic Wrappers

    12. Mixins Available in languages like Lisp with CLOS Allow the composition of functionality in a mixin with classes to create new classes Benefit: Provide a way to separate out pieces of similar functionality

    13. Mixins Drawback: Total ordering – composition is linear, with later mixins overriding earlier features with the same names Drawback: Dispersal of glue code – composition code is spread throughout mixin layers Drawback: Fragile hierarchies – modifying a mixin can impact other mixins earlier in the chain silently

    14. Mixin Example

    15. Introduction to Traits

    16. Properties of Traits A trait provides a set of methods that implement behavior. A trait requires a set of methods that serve as parameters for the provided behavior. Traits do not specify any state variables, and the methods provided by traits never access state variables directly. Classes and traits can be composed from other traits, but the composition order is irrelevant. Conflicting methods must be explicitly resolved.

    17. Properties of Traits Trait composition does not affect the semantics of a class: the meaning of the class is the same as it would be if all of the methods obtained from the trait(s) were defined directly in the class. Similarly, trait composition does not affect the semantics of a trait: a composite trait is equivalent to a flattened trait containing the same methods. …from page 3(250)

    18. Specifying Traits A trait contains the methods that it provides It also consists of the methods that it requires – since it does not contain state, is uses accessors provided in the code used to compose the traits with a class

    19. Example Traits

    20. Using Traits Class = Superclass + State + Traits + Glue A class provides it’s own state It also provides “glue”, which is the code that hooks the traits in Traits can satisfy each other’s requirements for accessors A class is complete if all of the trait’s requirements are met

    21. Composition Classes with traits can be flattened into a model of a class without traits Methods in traits have the same behavior as they would if they were implemented directly in the class The composition order is irrelevant; any conflicts must be specifically disambiguated Methods in traits override superclass methods, and class methods override trait methods Traits can be combined with other traits; these combinations need not be complete

    22. Example of Composition

    23. Dealing with Ambiguity There is a conflict when two methods with the same name appear in different traits A duplicate name can be aliased to another name Methods can also be excluded from a trait when it is composed

    24. More Complex Composition

    25. Traits and Inheritance

    26. Comparison with Single Inheritance Seen as complimentary Allows fine grained reuse Tool support is important, to give both flattened and “trait-aware” views

    27. Comparison with Single Inheritance Example from Smalltalk Refactoring of the collection classes to use traits resulted in 10% fewer methods, 12% less code Roughly 9% of the methods in the original were implemented “too high” so as to allow for reuse 48 traits, 567 methods all in all Some classes used up to 22 traits

    28. Comparison with Multiple Inheritance No conflicting state Names can still conflict – need to use aliases to resolve, and have a limited form of the diamond problem Aliases maintain “flattening”, since aliases appear as normal methods Traits can have a type of conflict (accessor name conflicts) that multiple inheritance does not Generic wrappers can be more easily specified

    29. Comparison with Mixins Ordering of trait composition is irrelevant If ordering is desired, subclasses can be formed to create partial orders Glue code is always present in the combining entity, and is not dispersed through intermediates (Their opinion) Traits are less fragile, since you don’t tend to get “ripple effects” with changes

    30. Additional Information

    31. Research in Traits Plan to introduce Traits into Java Will be somewhat different than Smalltalk version, since static typing may constrain what can be done Will need to compile traits into standard Java code and build tool support Work starting this semester

    32. Related Papers Traits: Composable Units of Behaviour http://www.iam.unibe.ch/~scg/Archive/Papers/Scha03aTraits.pdf Traits: The Formal Model http://www.iam.unibe.ch/~scg/Archive/Papers/Scha02cTraitsModel.pdf

    33. Related Papers Applying traits to the Smalltalk collection classes http://portal.acm.org/citation.cfm?doid=949305.949311 Statically typed traits (Kathleen Fisher, John Reppy) http://www.cs.uchicago.edu/research/publications/techreports/TR-2003-13

    34. Their future plans Continue research into the use of traits Extend tool support (right now it is spotty) Extend to other languages (statically typed languages such as Java, especially)

More Related