Upgrade to Pro — share decks privately, control downloads, hide ads and more …

OO without Inheritance

OO without Inheritance

My talk from @RedFrogConf – the Ruby Subconference

Lucas Dohmen

August 25, 2013
Tweet

More Decks by Lucas Dohmen

Other Decks in Programming

Transcript

  1. RWTH Aachen, Computer Science Student triAGENS GmbH, Developer moon lum

    moonbeamlabs by Lucas Dohmen OO Without Inheritance and the Treaty of Orlando
  2. Who is that uy? • Aran oDB Core Team •

    Aran oDB Foxx & Ruby Adapter • Student on the master branch • hacken.in & nerdkun.de /\ (~( ) ) /\_/\ ( _-----_(@ @) ( \ / /|/--\|\ V " " " "
  3. Software Architectures: Lessons • All the rules I learned are

    useless, because they were way to eneral • UML is not a ood fit for architectures • OO is not a ood idea
  4. OO is not a ood idea? • The professor is

    an emeritus prof • For him, the Smalltalkers were the new kids on the block • For him, OO == Inheritance • And it‘s quite interestin why
  5. Object = Identity + Behavior + State (stolen from Josh

    Susser) OOP = Objects + Encapsulation + Polymorphism + Inheritance
  6. class A end module M end class B < A

    include M end p B.ancestors # => [B, M, A, Object, Kernel, BasicObject]
  7. The “School Example” for Inheritance • There are birds •

    Birds can fly • A oose is a bird – it therefore can fly
  8. Implementation • We have an abstract class called Bird, it

    implements the fly method • We have a class Goose, it inherits from Bird
  9. Ok -.- • We introduce a new abstract class called

    BirdsThatCantFly inheritin from Bird • In this class we overwrite the fly method to raise an exception
  10. Come on… o_o • We introduce a new class called

    GooseWithWoundedWin inheritin from Goose • In this class we overwrite the fly method to raise an exception
  11. class CantFlyException < RuntimeError end class WoundedWing def fly raise

    CantFlyException, "Wing is wounded" end end class Wing def fly puts "If only I was an African swallow, I could carry a coconut." end end class Dove def initialize(wing) @wing = wing end def fly @wing.fly end end }We have Forwardable in the STD lib for that
  12. wing = Wing.new dove_1 = Dove.new(wing) dove_1.fly # => If

    only I was an African swallow, I could carry a coconut. broken_wing = WoundedWing.new dove_2 = Dove.new(broken_wing) dove_2.fly # => Wing is wounded (CantFlyException)
  13. Reuse • You can now reuse the Win class for

    different animals, includin birds • You can exchan e the wounded win with a healthy one when it ets cured • You can do win -size and wei ht specific calculations for speed (yep, yep)
  14. Ruby • In Java you would need to introduce interfaces

    now • Questionable result • But we‘re oose typin
  15. This was a simple example • But it applies too

    so many class hierarchies • Especially when requirements chan e • And requirements always chan e…
  16. What is the Treaty of Orlando? • It‘s a short

    paper from ’87 that unites the results of three papers • It‘s written by the authors of the papers themselves: Henry Liebermann, Lynn Andrea Stein and David Un ar • … and it ends with the sentence: “this a reement shall henceforth be known as the TREATY OF ORLANDO.” :)
  17. Dele ation is Inheritance • Dele ation is exactly the

    same thin as inheritance • It includes prototypical inheritance introduced in David Un ar‘s Self (now well known for its usa e in JavaScript)
  18. It‘s a classification • With the treaty of Orlando you

    can classify an inheritance / dele ation system • There are three attributes to distin uish
  19. Static or Dynamic? • Static systems require determinin the sharin

    patterns by the time an object is created • Dynamic systems permit determination of sharin patterns durin runtime, when an object actually receives a messa e
  20. Implicit or Explicit • Implicit systems dele ate anythin that

    can‘t be handled locally • Explicit systems have the ability to dele ate only a sin le method
  21. Per Object or Per Group • Per roup means that

    a roup of objects (a class f.e.) all inherit the same way • Per object means that the inheritance is defined for a specific object
  22. IMHO • Explicit inheritance helps a lot when you search

    for the method definition • Per object inheritance is much easier to mock • You can also chan e the dele ation tar et at runtime (when the win ets cured) • Therefore Ruby‘s Forwardable is the better inheritance mechanism in Ruby
  23. Inheritin class methods • Ruby has a beautiful feature: Class

    methods can be called in the body of a class • This is a buildin block for a lot of DSLs • I think there‘s nothin wron with that
  24. This is beautiful • … and you should use it

    • I would however wish that you could mark class methods as “protected” in Ruby
  25. What about Frameworks? • A difficult question • If you

    reimplement all hooks it needs, you have a lot of code duplication • But you still have the same problems with testin etc. • Keep the herita e small (Lookin at you, ActiveRecord)
  26. Wrappin Up • Is it still OOP if I don‘t

    use inheritance? I honestly don‘t know (Who cares) • Use inheritance wisely • Don‘t use inheritance for the thin s you were tau ht to use it for