SlideShare a Scribd company logo
Behavior Driven OOP
An Introduction to Traits
Inspiration Traits: Composable Units of Behavior
http://bit.ly/2eJfm7h
Oogway:
Talk: http://expert-talks.in/#designingWithTraitsInJava
Talk: https://www.youtube.com/watch?v=Tc6izy_oeus
Ruby-Gem: https://github.com/oogway/rubycube
Primer to Object Oriented Programming.
Classess Executable entity
Behaviour
State
Template/ Factory for objects
Instance generator
Type/ Entities
Definitions of method & Data.
Units of reuse:
Inheritance/ Hierarchy.
Composition.
Classess
Class(1)
Class(n)
Business
Problem
Entity
Class(2)
Decomposition
Composition
Inheritance Single
Multiple
Mixins
Problems with Inheritance
Single
Inheritance:
Duplication
Stream
ReadStream WriteStream
Read-Write-Stream
Duplication
read() write()
Single
Inheritance:
Inappropriate-
hierarchies
Stream
MyReader
WriteStream
Read-Write-Stream
read()
write()
ReadStream
Multiple
Inheritance:
Duplication
+ Rigidity
Reader A
read()
SyncReaderA
super.r
ead()
Locker
Reader B
read()
SyncReaderB
super.r
ead()
Locker
Multiple
Inheritance:
Duplicate
Wrappers
Reader A
read()
SyncReaderA
SyncReader
Reader B
read()
SyncReaderB
super.r
ead()
Locker
Nixon’s Diamond
Quakers are Pacifists, mostly.
Republicans are not Pacifists, mostly.
Nixon is both a Quaker and a Republican, certainly.
Is Nixon a pacifist?
Diamond
Problem
Collision
QObject
Rectangle
Button
Clickable
area() area()
area?
Mixins:
Total Ordering
Person
Quaker
Nixon
Republican
Pacifist? y
Pacifist? n
Pacifist? n
Mixins:
Linearity
module A
def as_string
super + ' Mixin-A'
end
end
module B
def as_string
super + ' Mixin-B'
end
end
class BaseA
def as_string
'BaseA'
end
end
class MyA < BaseA
include B
include A
end
$stderr.puts MyA.new.as_string
meson10@xps:~/workspace/meson10/traits$ ruby super.rb
BaseA Mixin-B Mixin-A
Mixins:
Fragile
Hierarchies
[½]
Person
Quaker
Nixon
Republican
Pacifist? Y
Votes_for? ??
Pacifist? N
Votes_for? Trump
Pacifist? N
Votes_for? Trump
Mixins:
Fragile
Hierarchies
[2/2]
Person
Republican
Nixon
Quaker
Pacifist? N
Votes_for? Trump
Pacifist? Y
Votes_for? ??
Pacifist? Y
Votes_for? ??
Traits
Traits
Building Blocks for Classes.
Primitive units of code reuse.
Specifying Traits
Provide method implementations.
Methods as parameters.
Cannot access State.
Cannot alter/maintain State.
Flattening property.
Traits: Implementations
Ruby
https://github.com/oogway/rubycube
Python
https://github.com/Debith/py3traits
Perl
Roles
http://www.modernperlbooks.com/mt/2009/05/perl-roles-versus-inheritance.html
Scala
Traits
Specification
require ‘cube’
Adder = Cube.interface {
proto(:sum, [Integer]) { Integer }
}
# Expect methods for parameters.
ProductCalcT = Cube.trait do
def product(a, b)
ret = 0
a.times { ret = sum([ret, b]) }
ret
end
# Enforce input.
requires_interface Adder
end
StatsCalcT = Cube.trait do
def avg(arr)
arr.reduce(0, &:+) / arr.size
end
end
Traits
Creating
Classes
Class = Superclass + State + Traits + Glue
SimpleCalc = Calc.with_trait(ProductCalcT)
sc = SimpleCalc.new
p sc.product(3, 2)
Or
AdvancedCalc = Calc
.with_trait(ProductCalcT)
.with_trait(StatsCalcT)
ac = AdvancedCalc.new
p ac.product(3, 2)
p ac.avg([3, 2, 4])
Traits
Conflict
Resolution [⅓]
ProductCalcT = Cube.trait do
def product(a, b)
ret = 0
a.times { ret = sum([ret, b]) }
ret
end
requires_interface Adder
end
StatsCalcT = Cube.trait do
def product; end
def avg(arr)
arr.reduce(0, &:+) / arr.size
end
end
AdvancedCalc = Calc.with_trait(ProductCalcT).with_trait(StatsCalcT)
AdvancedCalc.new.product(3, 2)
meson10@xps:$ bundle exec ruby traits.rb
/home/meson10/cube/traits.rb:38:in `append_features':
(Cube::Trait::MethodConflict)
#<UnboundMethod:
#<Class:0x005642a0cad580>(#<Module:0x005642a0cb5870>)#product>
ProductCalcT = Cube.trait do
def product(a, b)
ret = 0
a.times { ret = sum([ret, b]) }
ret
end
requires_interface Adder
end
StatsCalcT = Cube.trait do
def product; end
def avg(arr)
arr.reduce(0, &:+) / arr.size
end
end
AdvancedCalc = Calc.with_trait(ProductCalcT)
.with_trait(StatsCalcT, suppress: [:product])
P AdvancedCalc.new.product(3, 2)
meson10@xps:$ bundle exec ruby traits.rb
6
Traits
Conflict
Resolution [⅔]
class CalcImpl
def sum(a)
a.reduce(0, &:+)
end
# Trumps trait.
def product(a, b)
8
end
end
Calc = Cube.from(CalcImpl)
ProductCalcT = Cube.trait do
def product(a, b)
ret = 0
a.times { ret = sum([ret, b]) }
ret
end
requires_interface Adder
end
SimpleCalc = Calc.with_trait(ProductCalcT)
p SimpleCalc.new.product(3, 2)
Traits
Conflict
Resolution
[3/3]
ProductCalcT = Cube.trait do
def product(a, b)
ret = 0
a.times { ret = sum([ret, b]) }
ret
end
requires_interface Adder
end
PolarCalcT = Cube.trait do
def product(a, b)
# Polar implementation
88
end
end
AdvancedCalc = Calc
.with_trait(ProductCalcT)
.with_trait(PolarCalcT, rename: {"product" => "polar_product"})
ac = AdvancedCalc.new
p ac.product(3, 2)
p ac.polar_product(3, 2)
Traits
Aliasing
Traits
Conflict
Resolution
Class methods >> trait methods.
Trait methods >> superclass methods.
Method conflicts must be explicitly resolved.
Traits
Issue(s)
When two traits are composed, it may be that each
requires a semantically different method that
happens to have the same name.
Consider a trait X that uses two traits Y1 and Y2 ,
which in turn both use the trait Z . foo provided
by Z will be obtained by X twice. The key
language design question is: should this be
considered a conflict?
Thank you.

More Related Content

PDF
Ruby on Rails Oracle adaptera izstrāde
PDF
JavaScript ES6
PPTX
Advanced JavaScript
PDF
Letswift19-clean-architecture
ODP
JavaScript Web Development
ODP
ES6 PPT FOR 2016
PDF
AST Rewriting Using recast and esprima
PDF
フレームワークなしでWSGIプログラミング
Ruby on Rails Oracle adaptera izstrāde
JavaScript ES6
Advanced JavaScript
Letswift19-clean-architecture
JavaScript Web Development
ES6 PPT FOR 2016
AST Rewriting Using recast and esprima
フレームワークなしでWSGIプログラミング

What's hot (20)

PDF
AkJS Meetup - ES6++
PDF
Scala introduction
PDF
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
PPTX
AST - the only true tool for building JavaScript
PDF
Minimizing Decision Fatigue to Improve Team Productivity
PDF
Swift core
PDF
Opaque Pointers Are Coming
PDF
Planet-HTML5-Game-Engine Javascript Performance Enhancement
PDF
EcmaScript 6 - The future is here
PDF
Proxies are Awesome!
PPTX
ES6 Overview
PDF
Douglas Crockford: Serversideness
PDF
Writing Your App Swiftly
PDF
Introduction into ES6 JavaScript.
PDF
Introduction to asynchronous DB access using Node.js and MongoDB
PDF
Functional programming using underscorejs
PDF
Artem Yavorsky "99 ways to take away your ugly polyfills"
PDF
PDF
Rust ⇋ JavaScript
PDF
Javascript essential-pattern
AkJS Meetup - ES6++
Scala introduction
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
AST - the only true tool for building JavaScript
Minimizing Decision Fatigue to Improve Team Productivity
Swift core
Opaque Pointers Are Coming
Planet-HTML5-Game-Engine Javascript Performance Enhancement
EcmaScript 6 - The future is here
Proxies are Awesome!
ES6 Overview
Douglas Crockford: Serversideness
Writing Your App Swiftly
Introduction into ES6 JavaScript.
Introduction to asynchronous DB access using Node.js and MongoDB
Functional programming using underscorejs
Artem Yavorsky "99 ways to take away your ugly polyfills"
Rust ⇋ JavaScript
Javascript essential-pattern
Ad

Similar to Behavior driven oop (20)

PDF
TI1220 Lecture 8: Traits & Type Parameterization
PDF
Inheritance And Traits
PPT
fixtures_vs_AR_vs_factories.ppt
KEY
Traits composition
PDF
Hidden Gems of Ruby 1.9
PDF
Funtional Ruby - Mikhail Bortnyk
PDF
Functional Ruby
KEY
Frozen rails 2012 - Fighting Code Smells
PPTX
DCI
PDF
ScotRuby - Dark side of ruby
KEY
An introduction to Ruby
PDF
130705 zephyrin soh - how developers spend their effort during maintenance ...
PDF
Scientific Computing with Python Webinar: Traits
PDF
Testing Ruby with Rspec (a beginner's guide)
PDF
OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
KEY
Tdd for BT E2E test community
PDF
The Ring programming language version 1.2 book - Part 20 of 84
PPTX
Introduction to the Ruby Object Model
PDF
Measuring Aspect-Oriented Software In Practice
PDF
TI1220 Lecture 8: Traits & Type Parameterization
Inheritance And Traits
fixtures_vs_AR_vs_factories.ppt
Traits composition
Hidden Gems of Ruby 1.9
Funtional Ruby - Mikhail Bortnyk
Functional Ruby
Frozen rails 2012 - Fighting Code Smells
DCI
ScotRuby - Dark side of ruby
An introduction to Ruby
130705 zephyrin soh - how developers spend their effort during maintenance ...
Scientific Computing with Python Webinar: Traits
Testing Ruby with Rspec (a beginner's guide)
OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Tdd for BT E2E test community
The Ring programming language version 1.2 book - Part 20 of 84
Introduction to the Ruby Object Model
Measuring Aspect-Oriented Software In Practice
Ad

Recently uploaded (20)

PDF
Modernizing your data center with Dell and AMD
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
KodekX | Application Modernization Development
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation theory and applications.pdf
PPT
Teaching material agriculture food technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Network Security Unit 5.pdf for BCA BBA.
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Understanding_Digital_Forensics_Presentation.pptx
Modernizing your data center with Dell and AMD
Digital-Transformation-Roadmap-for-Companies.pptx
KodekX | Application Modernization Development
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Reach Out and Touch Someone: Haptics and Empathic Computing
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation theory and applications.pdf
Teaching material agriculture food technology
Review of recent advances in non-invasive hemoglobin estimation
Network Security Unit 5.pdf for BCA BBA.
The AUB Centre for AI in Media Proposal.docx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
20250228 LYD VKU AI Blended-Learning.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Understanding_Digital_Forensics_Presentation.pptx

Behavior driven oop

Editor's Notes

  • #20: A trait contains methods that implement the behaviour that it provides. A trait may require methods as parameters for the provided Behaviour. Traits cannot specify any state, and never access state directly. Trait methods can access state indirectly, using required methods. The purpose of traits is to decompose classes into reusable building blocks by pro- viding first-class representations for the different aspects of the behaviour of a class. Note that we use the term “aspect” to denote an independent, but not necessarily cross- cutting, concern. Traits differ from classes in that they do not define any kind of state, and that they can be composed using mechanisms other than inheritance.