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

Slides:



Advertisements
Similar presentations
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz.
Advertisements

Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz.
Object Oriented Programming
Reusable Classes.  Motivation: Write less code!
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
CSE341: Programming Languages Lecture 22 Multiple Inheritance, Interfaces, Mixins Dan Grossman Fall 2011.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
“Traits: Composable Units of Behavior” N. Schaerli, S. Ducasse, O. Nierstrasz, and A. Black, U Bern / OHSU, ECOOP 2003 “Traits: A Mechanism for Fine-grained.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Summer Semester 2006.
13. Traits. Selected literature  Cook. Interfaces and Specifications for the Smalltalk-80 Collection Classes. OOPSLA 1992  Taivalsaari. On the Notion.
12. Traits and Classboxes (and Changeboxes). © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 12.2 Roadmap  Problems with Inheritance 
Principles of Object-oriented Programming Programming Language Paradigms August 26, 2002.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 24: Dynamic Binding COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Chapter 10 Classes Continued
12. Traits and Classboxes. © Oscar Nierstrasz ST — Traits and Classboxes 12.2 Roadmap  Problems with Inheritance  Traits  Refactoring Collections with.
Inheritance Review/Recap. ClassA extends ClassB ClassA now inherits (can access and use) all public and protected elements of ClassB We can expect the.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
1 Advanced Abstraction Mechanisms  Mixins  Traits  Delegation.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
Inheritance using Java
Lecture 3 Casting Abstract Classes and Methods Interfaces.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Chai: Traits for Java-like Languages by Charles Smith, Sophia Drossopoulou.
Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.
Chapter 12 Support for Object oriented Programming.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Dynamic Binding Object-Oriented Programming Spring
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Coming up: Inheritance
Nathanael Schärli (SCG, University of Bern) and Andrew P. Black (CSE, Oregon Health & Science University) Nathanael Schärli (SCG, University of Bern) and.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2013.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
Variations on Inheritance Object-Oriented Programming Spring
1 Lecture 23: Dynamic Binding (Section ) CSCI 431 Programming Languages Fall 2002 A compilation of material developed by Felix Hernandez-Campos.
Module 9. Dealing with Generalization Course: Refactoring.
Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology.
Object Oriented Programming in Java Habib Rostami Lecture 2.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.
Sections Inheritance and Abstract Classes
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods James Wilcox Winter 2017.
Types of Programming Languages
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2018.
Advanced Programming Behnam Hatami Fall 2017.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2017.
Abstract Classes Page
Java Programming Course
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 23 Multiple Inheritance, Mixins, Interfaces, Abstract Methods Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Presentation transcript:

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

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

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

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

Reusability Problem Mult. Inheritance - Generic Wrapper

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

Reusability Problem Mixin Inheritance – Glue Code Glue code

Reusability Problem Mixin Inheritance – Fragile Hierarchy

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!

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

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

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

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

Traits Examples (cont.) Object subclass: #Circle instaceVariableNames: ‘ center radius ’ uses:{TCircle. TDrawing} initialize center := 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.)

Object subclass: #Circle instanceVariableNames: ‘ center radius rgb ’ uses: { {#circleHash -> #hash. #circleEqual: -> #=}. TDrawing. {#colorHash -> #hash. #colorEqual: -> #=}} Object subclass: #Circle instanceVariableNames: ‘ center radius rgb ’ uses: { TCircle. TDrawing. TColor - {#hash. #=} }

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

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

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

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