Docstoc

Traits

Document Sample
Traits Powered By Docstoc
					          Traits
by Nathanael Schärli, Stéphane
Ducasse, Oscar Nierstrasz and
       Andrew P. Black
                     Outline
   Reusability problems with inheritance
    • Single inheritance
    • Multiple inheritance
    • Mixin inheritance
   Traits
   Traits examples
   Design decisions
   Comparison with other inheritance
    methods
   Implementation & application
          Reusability Problem
           Single Inheritance
   Not expressive enough
   Code duplication
   Move methods higher on the
    hierarchy and disable them in the
    subclasses
   Interface doesn’t help
           Reusability Problem
           Multiple Inheritance
   Conflict features
    • Diamond inheritance
    • Conflict methods
    • Conflict state variables
   Access overridden features
    • super not enough
   Generic wrappers
        Reusability Problem
Mult. Inheritance - Generic Wrapper
           Reusability Problem
            Mixin Inheritance
   Total ordering due to linearization
    • But the total order may not exist!
   Dispersal of glue code
    • Adding mixins one at a time
   Fragile hierarchies
    • Silently override when adding a new
      method to one of the mixins
     Reusability Problem
Mixin Inheritance – Glue Code
         Glue code
        Reusability Problem
Mixin Inheritance – Fragile Hierarchy
          Reusability Problem
   Classes play two competing roles:
    • Generator of instances: complete
    • Unit of reuse: small


   Trait: unit of reuse! Small!
                 Traits
   Provides a set of methods
   Requires a set of methods
   No state variables
                  Traits (cont.)
   Composable
    • Order irrelevant
    • Explicitly resolve conflict
         Disable
         Alias
         Explicitly overriden
   Conflict resoving
    • Class methods over trait methods
    • Trait methods over superclass methods
               Traits (cont.)
   Flatten
    • Methods in a trait is the same as if
      they’re defined directly in the class
    • super mean the real super class!


   Class =
      Superclass + State + Traits + Glue
                Traits Examples




Trait named: #TDrawing uses: {}
 draw                               bounds
   ^ self drawOn: World canvas         self requirement
 refresh                            drawOn: aCanvas
   ^ self refreshOn: World canvas      self requirement
 refreshOn: aCanvas
   ……
Traits Examples (cont.)
               Object subclass: #Circle
                instaceVariableNames:
                   ‘center radius’
                uses:{TCircle. TDrawing}
                initialize
                  center := 0@0
                  radius := 50
                center
                  ^ center
                radius
                  ^ radius
                center: aPoint
                  center := aPoint
                radius: aNumber
                  radius := aNumber
                drawOn: aCanvas
                  aCanvas
                    fillOven: self bounds
                    color: Color black
Traits Examples (cont.)
        Traits Examples (cont.)
Object subclass: #Circle
  instanceVariableNames: ‘center radius rgb’
  uses: { TCircle @ {#circleHash -> #hash. #circleEqual: -> #=}.
           TDrawing.
           TColor @ {#colorHash -> #hash. #colorEqual: -> #=}}




Object subclass: #Circle
  instanceVariableNames: ‘center radius rgb’
  uses: { TCircle. TDrawing. TColor - {#hash. #=} }
            Design Decisions
   Untangling reusability and classes
    • Reuse traits, not classes
   Single inheritance
    • Extended with flatten added traits
   Aliasing
    • No ugly A::aMethod and B::aMethod
    • Methods move from trait to trait
    • Different from method renaming in Eiffel
       Design Decisions (cont.)
   Unintentional naming conflicts
    • No real solution when two traits require
      a semantically different methods with
      identical name and signature
   Conflict strategies
    • No states in traits: no location problem
    • No conflict if same method is obtained
      more than once via different path
    • Re-implement causes a conflict
Comparison With Other Inheritance
           Methods
   Conflicting features
   Accessing overridden/conflicting
    features
   Factoring out generic wrappers
   Total ordering
   Dispersal of glue code
   Fragile hierarchies
    Implementation & Application
   In Squeak, an open-source dialect of
    Smalltalk-80
   No change to the virtual machine
   Refactoring Smalltalk-80 collection
    hierarchy
    • 48 traits, 567 methods
    • Code 12% smaller (another 9% too high)
    • Composes as many as 22 traits

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:6
posted:8/12/2012
language:
pages:21