0
!quot;#$




Modularity beyond
  inheritance
      Alexandre Bergel
     RMoD team, INRIA,
         Lille, France
    alex...
Goal of this lecture



 •   To introduce research problems related to class-inheritance

 •   Present 2 state-of-the-art ...
Sources & references
 •   Wirfs-Brock & McKean, Object Design — Roles,
     Responsibilities and Collaborations, 2003.

 •...
Outline


1. Inheritance
2. Classboxes: evolving behavior
3. Traits: composing behavior
4. Concluding words
Inheritance


 •   Inheritance in object-oriented programming languages is a
     mechanism to:

     -   derive new subcl...
Inheritance mechanisms

•   OO languages realize inheritance in different ways:

    -   self: dynamically access subclass...
Classboxes for evolution


 I. Problem: AWT and Swing anomalies
 II. Model: Classbox/J
 III. Solution: Swing as a classbox...
Presentation of AWT
 java.awt

                                   Component


                                    Containe...
Broken Inheritance in Swing
 java.awt

                        Component


                        Container    Button
   ...
Problem: Code Duplication
                                                         Code Duplication
 java.awt

           ...
Problem: explicit type operation
public class Container extends Component {
  Component components[] = new Component [0];
...
Alternative to inheritance

•   AWT couldn’t be enhanced without risk of breaking existing
    code.


•   Swing is, there...
Classbox/J

 •   Module system for Java allowing classes to be refined
     without breaking former clients.

 •   A classb...
Swing Refactored as a Classbox
  AwtCB

                                         Component


                             ...
Swing Refactored as a Classbox
 AwtCB

                                    Component


                               Cont...
Swing Refactoring

 •   6500 lines of code refactored over 4 classes.

 •   Inheritance defined in AwtCB is fully preserved...
Properties of Classboxes

 •   Minimal extension of the Java syntax (transitive import,
     refine and original keywords)....
Traits


 I. Problem: Stream in Squeak anomalies
 II. Model: Traits
 III. Solution: Refactoring with Traits
 IV. Ongoing w...
Stream in Squeak



 •   Example of a library that has been in use for almost 20
     years

 •   Contains many flaws in it...
Stream in Squeak
                                                                 FileStream
                             ...
Methods too high
                                                                 FileStream
                             ...
Methods too high


•   The nextPut: method defined in Stream allows for element
    addition

•   The ReadStream class is r...
Unused state
                                                                 FileStream
                                 ...
Unused state



•   State defined in the super classes are becoming irrelevant in
    subclasses

•   FileStream does not u...
Multiple inheritance simulation
                                                                 FileStream
              ...
Multiple inheritance




 •   Methods are duplicated among different class hierarchies
Class responsibilities



 •   Too many responsibilities for classes

     -   object factories

     -   group methods wh...
Class schizophrenia?



 •   Too many responsibilities for classes

     -   object factories => need for completeness

  ...
Traits

 •   Traits are parameterized behaviors
     -  traits provide a set of methods
     -  traits require a set of me...
Class = Superclass + State + Traits + Glue Methods




    •   Traits are the behavioral building blocks of classes

     ...
Composition rules

 Class methods take precedence over trait methods

  ColoredCircle                   ColoredCircle

   ...
Conflicts are explicitly resolved

 •     Override the conflict with a glue method
     -   Aliases provide access to the co...
Flattening property

Structured view                             Flat view
     Object                 equivalent         ...
Stream revisited
  Core                                                   TStream
                                        ...
Concluding words



•   Hard topic where proving a better solution requires a
    significant effort

•   Other hot topics:...
2008 Sccc Inheritance
Upcoming SlideShare
Loading in...5
×

2008 Sccc Inheritance

1,029

Published on

After a short introduction to object orientation, this lecture gives an overview of the Smalltalk programming language.
This lecture was given on November 10, 2008, during the summer school on programming languages (http://pleiad.dcc.uchile.cl/pl2008/)

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,029
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "2008 Sccc Inheritance"

  1. 1. !quot;#$ Modularity beyond inheritance Alexandre Bergel RMoD team, INRIA, Lille, France alexandre@bergel.eu
  2. 2. Goal of this lecture • To introduce research problems related to class-inheritance • Present 2 state-of-the-art research topics related to class inheritance
  3. 3. Sources & references • Wirfs-Brock & McKean, Object Design — Roles, Responsibilities and Collaborations, 2003. • Alexandre Bergel, Stéphane Ducasse, and Oscar Nierstrasz, Classbox/J: Controlling the Scope of Change in Java, OOPSLA'05 • Damien Cassou, Stéphane Ducasse and Roel Wuyts, Traits at Work: the design of a new trait-based stream library, In Journal of Computer Languages, Systems and Structures, Elsevier, 2008 • Stephane Ducasse, Roel Wuts, Alexandre Bergel, and Oscar Nierstrasz, User-Changeable Visibility: Resolving Unanticipated Name Clashes in Traits, OOPSLA'07
  4. 4. Outline 1. Inheritance 2. Classboxes: evolving behavior 3. Traits: composing behavior 4. Concluding words
  5. 5. Inheritance • Inheritance in object-oriented programming languages is a mechanism to: - derive new subclasses from existing classes - where subclasses inherit all the features from their parent(s) - and may selectively override the implementation of some features.
  6. 6. Inheritance mechanisms • OO languages realize inheritance in different ways: - self: dynamically access subclass methods - super: statically access overridden, inherited methods - multiple inheritance: inherit features from multiple superclasses - abstract classes: partially defined classes (to inherit from only) - mixins: build classes from partial sets of features - interfaces: specify method argument and return types - subtyping: guarantees that subclass instances can be substituted
  7. 7. Classboxes for evolution I. Problem: AWT and Swing anomalies II. Model: Classbox/J III. Solution: Swing as a classbox IV. Ongoing work: general scoping mechanism (not presented)
  8. 8. Presentation of AWT java.awt Component Container Button Window Frame • In the AWT framework: - Widgets are components (i.e., inherit from Component) - A frame is a window (Frame is a subclass of Window)
  9. 9. Broken Inheritance in Swing java.awt Component Container Button Window Frame javax.swing JComponent JFrame JWindow JButton
  10. 10. Problem: Code Duplication Code Duplication java.awt Component Container Button Window Frame javax.swing JComponent JFrame JWindow accessibleCont accessibleContext accessibleContext ext update() JButton rootPane rootPane update() update() setLayout() setLayout() ... ...
  11. 11. Problem: explicit type operation public class Container extends Component { Component components[] = new Component [0]; public Component add (Component comp) {...} } public class JComponent extends Container { public void paintChildren (Graphics g) { for (; i>=0 ; i--) { Component comp = getComponent (i); isJComponent = (comp instanceof JComponent); ... ((JComponent) comp).getBounds(); } }}
  12. 12. Alternative to inheritance • AWT couldn’t be enhanced without risk of breaking existing code. • Swing is, therefore, built on the top of AWT using subclassing. • As a result, Swing is a big mess internally! • We need an alternative to inheritance to support unanticipated changes.
  13. 13. Classbox/J • Module system for Java allowing classes to be refined without breaking former clients. • A classbox is like a package where: - a class defined or imported within a classbox p can be imported by another classbox (transitive import). - class members can be added or redefined on an imported class with the keyword refine. - a refined method can access its original behavior using the original keyword
  14. 14. Swing Refactored as a Classbox AwtCB Component Container Button Window Frame javax.swing JComponent JFrame JWindow accessibleContext accessibleContext accessibleContext ext update() JButton rootPane rootPane update() update() setLayout() setLayout() ... ...
  15. 15. Swing Refactored as a Classbox AwtCB Component Container Button Window Frame SwingCB Component Frame Window accessibleContext component rootPane setLayout() update() add(Component) Button setRootPane() remove(Component) setContentPane() ...
  16. 16. Swing Refactoring • 6500 lines of code refactored over 4 classes. • Inheritance defined in AwtCB is fully preserved in SwingCB: - In SwingCB, every widget is a component (i.e., inherits from the extended AWT Component). - The property “a frame is a window” is true in SwingCB. • Removed duplicated code: the refined Frame is 29 % smaller than the original JFrame. • Explicit type checks like obj instanceof JComponent and (JComponent)obj are avoided.
  17. 17. Properties of Classboxes • Minimal extension of the Java syntax (transitive import, refine and original keywords). • Refinements are confined to the classbox that define them and to classboxes that import refined classes. • Method redefinitions have precedence over previous definitions. • Classes can be refined without risk of breaking former clients.
  18. 18. Traits I. Problem: Stream in Squeak anomalies II. Model: Traits III. Solution: Refactoring with Traits IV. Ongoing work: Pure trait language (not presented)
  19. 19. Stream in Squeak • Example of a library that has been in use for almost 20 years • Contains many flaws in its conception
  20. 20. Stream in Squeak FileStream rwmode WriteStream Positionable name writeLimit Stream fileID contents collection buffer1 flush Stream position atEnd next atEnd readLimit close nextPut: close atEnd flush nextPutAll: contents contents fullName position: do: isEmpty next reset flush next: next: setToEnd next peek nextPut: size next: position nextPutAll: space next:put: position: ReadStream peek cr nextPut: reset next position nextPutAll: setToEnd next: position: upToEnd skip: nextPut: ReadWriteStream reset skipTo: size close setToEnd upTo: upTo: contents size upToEnd upToEnd next skip: next: upTo: upToEnd
  21. 21. Methods too high FileStream rwmode WriteStream Positionable name writeLimit Stream fileID contents collection buffer1 flush Stream position atEnd next atEnd readLimit close nextPut: close atEnd flush nextPutAll: contents contents fullName position: do: isEmpty next reset flush next: next: setToEnd next peek nextPut: size next: position nextPutAll: space next:put: position: ReadStream peek cr nextPut: reset next position nextPutAll: setToEnd next: position: upToEnd skip: nextPut: ReadWriteStream reset skipTo: size close setToEnd upTo: upTo: contents size upToEnd upToEnd next skip: nextPutAll: aCollection next: upTo: upToEnd aCollection do: [:v| self nextPut: v]. ^ aCollection
  22. 22. Methods too high • The nextPut: method defined in Stream allows for element addition • The ReadStream class is read-only • It therefore needs to “cancel” this method by redefining it and throwing an exception
  23. 23. Unused state FileStream rwmode WriteStream Positionable name writeLimit Stream fileID contents collection buffer1 flush Stream position atEnd next atEnd readLimit close nextPut: close atEnd flush nextPutAll: contents contents fullName position: do: isEmpty next reset flush next: next: setToEnd next peek nextPut: size next: position nextPutAll: space next:put: position: ReadStream peek cr nextPut: reset next position nextPutAll: setToEnd next: position: upToEnd skip: nextPut: ReadWriteStream reset skipTo: size close setToEnd upTo: upTo: contents size upToEnd upToEnd next skip: next: upTo: upToEnd
  24. 24. Unused state • State defined in the super classes are becoming irrelevant in subclasses • FileStream does not use inherited variables
  25. 25. Multiple inheritance simulation FileStream rwmode WriteStream Positionable name writeLimit Stream fileID contents collection buffer1 flush Stream position atEnd next atEnd readLimit close nextPut: close atEnd flush nextPutAll: contents contents fullName position: do: isEmpty next reset flush next: next: setToEnd next peek nextPut: size next: position nextPutAll: space next:put: position: ReadStream peek cr nextPut: reset next position nextPutAll: setToEnd next: position: upToEnd skip: nextPut: ReadWriteStream reset skipTo: size close setToEnd upTo: upTo: contents size upToEnd upToEnd next skip: next: upTo: upToEnd
  26. 26. Multiple inheritance • Methods are duplicated among different class hierarchies
  27. 27. Class responsibilities • Too many responsibilities for classes - object factories - group methods when subclassing `
  28. 28. Class schizophrenia? • Too many responsibilities for classes - object factories => need for completeness - group methods when subclassing => need to incorporate incomplete fragments
  29. 29. Traits • Traits are parameterized behaviors - traits provide a set of methods - traits require a set of methods - traits are purely behavioral TCircle area radius bounds radius: diameter center hash center: ...
  30. 30. Class = Superclass + State + Traits + Glue Methods • Traits are the behavioral building blocks of classes Generic properties Object TColor TRectangle Component Geometrical ... RectangleWidget RectangleShape x1, y1, x2, y2 TColor point1, point2 setX1(...) setX1(...) setY1(...) TRectangle setY1(...)
  31. 31. Composition rules Class methods take precedence over trait methods ColoredCircle ColoredCircle TCircle TCircle draw draw draw radius radius TColor TColor hue hue rgb rgb
  32. 32. Conflicts are explicitly resolved • Override the conflict with a glue method - Aliases provide access to the conflicting methods • Avoid the conflict - Exclude the conflicting method from one trait ColoredCircle ColoredCircle TCircle TCircle hash -> circleHash hash -> circleHash hash TColor TColor hash -> colorHash hash
  33. 33. Flattening property Structured view Flat view Object equivalent Object ColoredCircle ColoredCircle TCircle draw draw draw radius radius TColor hue hue rgb rgb
  34. 34. Stream revisited Core TStream binary close closed isBinary isClosed isStream TGettableStream do: atEnd TPositionableStream TPuttableStream nextMatchFor: next atEnd position nextPutAll: nextPut: next: peek atStart setPosition: next:put: peekFor: outputCollectionClass back size print: skip: close flush skipTo: isEmpty upTo: position: upToEnd reset upToElementSatisfying: setToEnd @ {#basicBack->#back} TGettablePositionableStream TPuttablePositionableStream back writeBack backUpTo: match: nextDelimited: skip:
  35. 35. Concluding words • Hard topic where proving a better solution requires a significant effort • Other hot topics: Virtual classes, Nested inheritance, Selector namespaces
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×