50
OOPS v2.0.0-rc.1 Aditya Godbole @aagdbl

OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Embed Size (px)

Citation preview

Page 1: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

OOPS v2.0.0-rc.1Aditya Godbole

@aagdbl

Page 2: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Definitions

• Type - Set of methods

• Subtype(T) - Superset methods of the type T

• Class - Implementation of type + properties of implementing object

Page 3: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Maintainability

Page 4: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Maintainability

Re-use

Page 5: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Maintainability

Re-use

of?

Page 6: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Page 7: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Behaviour

(not data)

Page 8: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Maintainability

Page 9: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Maintainability

• Re-use of behaviour

Page 10: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Maintainability

• Re-use of behaviour

• Typing and Sub-typing

Page 11: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Axes of maintainability

Page 12: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Axes of maintainability

Don’t repeatyourself(DRY)

Page 13: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Axes of maintainability

Don’t repeatyourself(DRY)

Page 14: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Axes of maintainability

Don’t repeatyourself(DRY)

Don’t shoot yourself(DSY)

Page 15: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Maintainability

• Re-use of behaviour

• Sub-typing

Page 16: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Maintainability

• Re-use of behaviour

• Sub-typing Granularity

Page 17: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Page 18: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Composability

• Behaviour - Inject behaviour from multiple sources

• Types - Create superset types by combining smaller types

Page 19: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Classes

• Re-use

• Multiple inheritance

• Subtyping (Polymorphism)

• Multiple inheritance

Page 20: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Interfaces

Page 21: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Interfaces

• Type definitions

Page 22: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Interfaces

• Type definitions

• Composable

Page 23: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Classes / Inheritance

InterfacesJava?

Page 24: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Classes / Inheritance

Interfaces

Mixins /Modules

Java?

Page 25: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Classes / Inheritance

Interfaces

Mixins /Modules

Java?

Ruby?

Page 26: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Classes / Inheritance

Interfaces

Mixins /Modules

Java?

Page 27: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Classes / Inheritance

Interfaces

Mixins /Modules

Java?

Ruby?

Page 28: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Classes / Inheritance

Interfaces

Mixins /Modules

Java?

Ruby?Interfaces?

Page 29: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

gem install rubycube

require “cube”

Composable Units of BEhaviour

Page 30: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Page 31: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Page 32: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Classes / Inheritance

Interfaces

Mixins /Modules

Java?

RubyInterfaces

Page 33: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Classes / Inheritance

Interfaces

Mixins /Modules

Java?

Page 34: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Classes / Inheritance

Interfaces

Mixins /Modules

Java?

RubyInterfaces

Page 35: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Classes / Inheritance

Interfaces

Mixins /Modules

Java?

RubyInterfaces

??

Page 36: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Conflicts

Page 37: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Page 38: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Page 39: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

required interface

Page 40: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Page 41: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

(sum, pos, fact)

Page 42: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Page 43: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

AdvancedCalc =

SimpleCalcImpl

ProductCalcT

StatsCalcT - [:product]

+

+

=> AdvancedCalculator

Page 44: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Page 45: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby
Page 46: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Using Traits• Structural classes - Enforce structural integrity.

• Identify interactions - Create smallest possible interface

• Traits should be smaller or equal to interfaces

• If required - extend structural class to create glue class for required interface

• Compose using traits

Page 47: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Specs

• Rspec “shared_examples”

• Compose specs using “it_behaves_like”

Page 48: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

Also achievable …

• Java 8+

• Golang

Page 49: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

DRY

DSY

Java 8

InterfacesTraits

Ruby Go

Page 50: OOPS v2.0.0-rc.1 - Interfaces and Traits for Ruby

__/\__

https://github.com/adityagodbole/rubycube

gem install rubycube

(please go and read the traits paper)