Presentation is loading. Please wait.

Presentation is loading. Please wait.

Traits by Nathanael Sch ä rli, St é phane Ducasse, Oscar Nierstrasz and Andrew P. Black.

Similar presentations


Presentation on theme: "Traits by Nathanael Sch ä rli, St é phane Ducasse, Oscar Nierstrasz and Andrew P. Black."— Presentation transcript:

1 Traits by Nathanael Sch ä rli, St é phane Ducasse, Oscar Nierstrasz and Andrew P. Black

2 Outline Reusability problems with inheritance Reusability problems with inheritance Single inheritanceSingle inheritance Multiple inheritanceMultiple inheritance Mixin inheritanceMixin inheritance Traits Traits Traits examples Traits examples Design decisions Design decisions Comparison with other inheritance methods Comparison with other inheritance methods Implementation & application Implementation & application

3 Reusability Problem Single Inheritance Not expressive enough Not expressive enough Code duplication Code duplication Move methods higher on the hierarchy and disable them in the subclasses Move methods higher on the hierarchy and disable them in the subclasses Interface doesn ’ t help Interface doesn ’ t help

4 Reusability Problem Multiple Inheritance Conflict features Conflict features Diamond inheritanceDiamond inheritance Conflict methodsConflict methods Conflict state variablesConflict state variables Access overridden features Access overridden features not enoughsuper not enough Generic wrappers Generic wrappers

5 Reusability Problem Mult. Inheritance - Generic Wrapper

6 Reusability Problem Mixin Inheritance Total ordering due to linearization Total ordering due to linearization But the total order may not exist!But the total order may not exist! Dispersal of glue code Dispersal of glue code Adding mixins one at a timeAdding mixins one at a time Fragile hierarchies Fragile hierarchies Silently override when adding a new method to one of the mixinsSilently override when adding a new method to one of the mixins

7 Reusability Problem Mixin Inheritance – Glue Code Glue code

8 Reusability Problem Mixin Inheritance – Fragile Hierarchy

9 Reusability Problem Classes play two competing roles: Classes play two competing roles: Generator of instances: completeGenerator of instances: complete Unit of reuse: smallUnit of reuse: small Trait: unit of reuse! Small! Trait: unit of reuse! Small!

10 Traits Provides a set of methods Provides a set of methods Requires a set of methods Requires a set of methods No state variables No state variables

11 Traits (cont.) Composable Composable Order irrelevantOrder irrelevant Explicitly resolve conflictExplicitly resolve conflict Disable Disable Alias Alias Explicitly overriden Explicitly overriden Conflict resoving Conflict resoving Class methods over trait methodsClass methods over trait methods Trait methods over superclass methodsTrait methods over superclass methods

12 Traits (cont.) Flatten Flatten Methods in a trait is the same as if they ’ re defined directly in the classMethods in a trait is the same as if they ’ re defined directly in the class mean the real super class!super mean the real super class! Class = Superclass + State + Traits + Glue Class = Superclass + State + Traits + Glue

13 Traits Examples Trait named: #TDrawing uses: {} draw ^ self drawOn: World canvas refresh ^ self refreshOn: World canvas refreshOn: aCanvas …… bounds self requirement drawOn: aCanvas self requirement

14 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

15 Traits Examples (cont.)

16 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. #=} }

17 Design Decisions Untangling reusability and classes Untangling reusability and classes Reuse traits, not classesReuse traits, not classes Single inheritance Single inheritance Extended with flatten added traitsExtended with flatten added traits Aliasing Aliasing No ugly A::aMethod and B::aMethodNo ugly A::aMethod and B::aMethod Methods move from trait to traitMethods move from trait to trait Different from method renaming in EiffelDifferent from method renaming in Eiffel

18 Design Decisions (cont.) Unintentional naming conflicts Unintentional naming conflicts No real solution when two traits require a semantically different methods with identical name and signatureNo real solution when two traits require a semantically different methods with identical name and signature Conflict strategies Conflict strategies No states in traits: no location problemNo states in traits: no location problem No conflict if same method is obtained more than once via different pathNo conflict if same method is obtained more than once via different path Re-implement causes a conflictRe-implement causes a conflict

19 Comparison With Other Inheritance Methods Conflicting features Conflicting features Accessing overridden/conflicting features Accessing overridden/conflicting features Factoring out generic wrappers Factoring out generic wrappers Total ordering Total ordering Dispersal of glue code Dispersal of glue code Fragile hierarchies Fragile hierarchies

20 Implementation & Application In Squeak, an open-source dialect of Smalltalk-80 In Squeak, an open-source dialect of Smalltalk-80 No change to the virtual machine No change to the virtual machine Refactoring Smalltalk-80 collection hierarchy Refactoring Smalltalk-80 collection hierarchy 48 traits, 567 methods48 traits, 567 methods Code 12% smaller (another 9% too high)Code 12% smaller (another 9% too high) Composes as many as 22 traitsComposes as many as 22 traits

21


Download ppt "Traits by Nathanael Sch ä rli, St é phane Ducasse, Oscar Nierstrasz and Andrew P. Black."

Similar presentations


Ads by Google