SlideShare ist ein Scribd-Unternehmen logo
1 von 30
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.

Weitere ähnliche Inhalte

Was ist angesagt?

AST - the only true tool for building JavaScript
AST - the only true tool for building JavaScriptAST - the only true tool for building JavaScript
AST - the only true tool for building JavaScript
Ingvar Stepanyan
 
Introduction to asynchronous DB access using Node.js and MongoDB
Introduction to asynchronous DB access using Node.js and MongoDBIntroduction to asynchronous DB access using Node.js and MongoDB
Introduction to asynchronous DB access using Node.js and MongoDB
Adrien Joly
 
Functional programming using underscorejs
Functional programming using underscorejsFunctional programming using underscorejs
Functional programming using underscorejs
偉格 高
 

Was ist angesagt? (20)

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

Ähnlich wie Behavior driven oop

JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
David Padbury
 
Fast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on OracleFast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on Oracle
Raimonds Simanovskis
 
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
Nick Sieger
 
PofEAA and SQLAlchemy
PofEAA and SQLAlchemyPofEAA and SQLAlchemy
PofEAA and SQLAlchemy
Inada Naoki
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e big
Andy Peterson
 
Active Record Inheritance in Rails
Active Record Inheritance in RailsActive Record Inheritance in Rails
Active Record Inheritance in Rails
Sandip Ransing
 

Ähnlich wie Behavior driven oop (20)

Design Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron PattersonDesign Summit - Rails 4 Migration - Aaron Patterson
Design Summit - Rails 4 Migration - Aaron Patterson
 
Adventurous Merb
Adventurous MerbAdventurous Merb
Adventurous Merb
 
Connecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRubyConnecting the Worlds of Java and Ruby with JRuby
Connecting the Worlds of Java and Ruby with JRuby
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)
2013.02.02 지앤선 테크니컬 세미나 - Xcode를 활용한 디버깅 팁(OSXDEV)
 
Tikal's Backbone_js introduction workshop
Tikal's Backbone_js introduction workshopTikal's Backbone_js introduction workshop
Tikal's Backbone_js introduction workshop
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 
Fast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on OracleFast Web Applications Development with Ruby on Rails on Oracle
Fast Web Applications Development with Ruby on Rails on Oracle
 
Riak at The NYC Cloud Computing Meetup Group
Riak at The NYC Cloud Computing Meetup GroupRiak at The NYC Cloud Computing Meetup Group
Riak at The NYC Cloud Computing Meetup Group
 
SCALA - Functional domain
SCALA -  Functional domainSCALA -  Functional domain
SCALA - Functional domain
 
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
JRuby + Rails = Awesome Java Web Framework at Jfokus 2011
 
Oracle adapters for Ruby ORMs
Oracle adapters for Ruby ORMsOracle adapters for Ruby ORMs
Oracle adapters for Ruby ORMs
 
"Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin "Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
 
MADlib Architecture and Functional Demo on How to Use MADlib/PivotalR
MADlib Architecture and Functional Demo on How to Use MADlib/PivotalRMADlib Architecture and Functional Demo on How to Use MADlib/PivotalR
MADlib Architecture and Functional Demo on How to Use MADlib/PivotalR
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
 
PofEAA and SQLAlchemy
PofEAA and SQLAlchemyPofEAA and SQLAlchemy
PofEAA and SQLAlchemy
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e big
 
Active Record Inheritance in Rails
Active Record Inheritance in RailsActive Record Inheritance in Rails
Active Record Inheritance in Rails
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developer
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Behavior driven oop

Hinweis der Redaktion

  1. 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.