0
OO Systems and Roles
 Curtis “Ovid” Poe
 Senior Software Engineer
Copyright 2009 by Curtis “Ovid” Poe.

This presentation ...
Not a Tutorial
• #ovidfail
• “How” is easy
• “Why” is not




 Future Media & Technology         BBC MMIX
The Problem Domain

• 5,613 Brands
• 6,755 Series
• 386,943 Episodes
• 394,540 Versions
• 1,106,246 Broadcasts
• 1,701,309...
A Brief History of Pain

•Simula 67
 –Classes
 –Polymorphism
 –Encapsulation
 –Inheritance

 Future Media & Technology   ...
Multiple Inheritance

•C++
•Eiffel
•Perl
•CLOS

 Future Media & Technology     BBC MMIX
Single Inheritance

•C#
•Java
•BETA
•Ruby

 Future Media & Technology      BBC MMIX
Handling Inheritance

•Liskov
•Strict equivalence
•Interfaces
•Mixins
•C3
 Future Media & Technology    BBC MMIX
Four Decades of Pain

•Code smell
 –In the language!




 Future Media & Technology    BBC MMIX
B:: Object Hierarchy




Future Media & Technology    BBC MMIX
A Closer Look




Future Media & Technology       BBC MMIX
A Closer Look

• Multiple Inheritance




 Future Media & Technology       BBC MMIX
B::PVIV Pseudo-Code

• B::PVIV Internals

 bless => {
   pv => 'three',
   iv => 3,
 } => 'B::PVIV';

 Future Media & Tech...
Printing Numbers?
• Perl
 my $number = 3;
 $number   += 2;
 print "I have $number apples";

• Java
 int number = 3;
 numbe...
Class Details

• More pseudo-code
 package B::PVIV;
 use parent qw( B::PV B::IV );

 sub B::PV::as_string { $_[0]->{pv} }
...
Real World Pain




Future Media & Technology      BBC MMIX
Real World Pain




Future Media & Technology      BBC MMIX
Real World Pain




Future Media & Technology      BBC MMIX
Real World Pain




Future Media & Technology      BBC MMIX
Systems Grow




Future Media & Technology     BBC MMIX
The Problem

• Responsibility
  –Wants larger classes
• Reuse
  –Wants smaller classes



 Future Media & Technology      ...
Solution




Decouple!
Future Media & Technology           BBC MMIX
Solutions

• Interfaces
   –Reimplementing




 Future Media & Technology          BBC MMIX
Solutions

• Delegation
  –Scaffolding
  –Communication




 Future Media & Technology          BBC MMIX
Solutions

• Mixins
  –Ordering




 Future Media & Technology          BBC MMIX
PracticalJoke

• Needs:
  –explode()
  –fuse()




 Future Media & Technology    BBC MMIX
Shared Behavior
                   Method       Description


✓        Bomb::fuse()        Deterministic


         Spouse...
Ruby Mixins
  module Bomb
      def explode
          puts "Bomb explode"
      end
      def fuse
          puts "Bomb fu...
Ruby Mixins
    class PracticalJoke
        include Spouse
        include Bomb
    end

    joke = PracticalJoke.new()
  ...
Ruby Mixins
    class PracticalJoke
        include Spouse
        include Bomb
    end

    joke = PracticalJoke.new()
  ...
Moose Roles
{
    package Bomb;
    use Moose::Role;
    sub fuse    { say "Bomb explode" }
    sub explode { say "Bomb fu...
Moose Roles
      {
          package PracticalJoke;
          use Moose;
          with qw(Bomb Spouse);
      }
      my...
Moose Roles
      {
          package PracticalJoke;
          use Moose;
          with qw(Bomb Spouse);
      }
      my...
Moose Roles
    {
        package PracticalJoke;
        use Moose;
        with 'Bomb'   => { excludes => 'explode' },
  ...
Moose Roles
  package PracticalJoke;
  use Moose;
  with 'Bomb'   => { excludes    => 'explode' },
       'Spouse' => { ex...
Role Example
  package Does::Serialize::YAML
  use Moose::Role;
  use YAML::Syck;

  requires 'as_hash';

  sub serialize ...
Back To Work




Future Media & Technology       BBC MMIX
Work + Roles




Future Media & Technology       BBC MMIX
Work + Roles



package Country;
use Moose;
extends "My::ResultSource";
with qw(DoesStatic DoesAuditing);




Future Media...
Old BBC ResultSet Classes




 Future Media & Technology    BBC MMIX
New ResultSet Classes




Future Media & Technology    BBC MMIX
Work + Roles

       package BBC::Programme::Episode;
       use Moose;
       extends 'BBC::ResultSet';
       with qw(
 ...
Conclusion

• Increases comprehension
• Increases safety
• Decreases complexity




 Future Media & Technology        BBC...
Upcoming SlideShare
Loading in...5
×

Inheritance Versus Roles

3,263

Published on

This is an expanded version of my "Refactoring with Roles" talk. People don't ask me how to use roles. They're easy to use. They ask me why to use roles. That's pretty easy to, so long as you understand what's wrong with inheritance. This talk covers that.

A paper for this talk is at http://www.slideshare.net/Ovid/inheritance-versus-roles-1799996

Published in: Technology, Business
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,263
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
42
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "Inheritance Versus Roles"

  1. 1. OO Systems and Roles Curtis “Ovid” Poe Senior Software Engineer Copyright 2009 by Curtis “Ovid” Poe. This presentation is free and you may redistribute it and/or modify it under the terms of the GNU Free Documentation License. Template Copyright 2009 by BBC. Future Media & Technology  BBC MMIX
  2. 2. Not a Tutorial • #ovidfail • “How” is easy • “Why” is not Future Media & Technology  BBC MMIX
  3. 3. The Problem Domain • 5,613 Brands • 6,755 Series • 386,943 Episodes • 394,540 Versions • 1,106,246 Broadcasts • 1,701,309 On Demands July 2, 2009 Future Media & Technology  BBC MMIX
  4. 4. A Brief History of Pain •Simula 67 –Classes –Polymorphism –Encapsulation –Inheritance Future Media & Technology  BBC MMIX
  5. 5. Multiple Inheritance •C++ •Eiffel •Perl •CLOS Future Media & Technology  BBC MMIX
  6. 6. Single Inheritance •C# •Java •BETA •Ruby Future Media & Technology  BBC MMIX
  7. 7. Handling Inheritance •Liskov •Strict equivalence •Interfaces •Mixins •C3 Future Media & Technology  BBC MMIX
  8. 8. Four Decades of Pain •Code smell –In the language! Future Media & Technology  BBC MMIX
  9. 9. B:: Object Hierarchy Future Media & Technology  BBC MMIX
  10. 10. A Closer Look Future Media & Technology  BBC MMIX
  11. 11. A Closer Look • Multiple Inheritance Future Media & Technology  BBC MMIX
  12. 12. B::PVIV Pseudo-Code • B::PVIV Internals bless => { pv => 'three', iv => 3, } => 'B::PVIV'; Future Media & Technology  BBC MMIX
  13. 13. Printing Numbers? • Perl my $number = 3; $number += 2; print "I have $number apples"; • Java int number = 3; number += 2; System.out.println( "I have " + number + " apples" ); Future Media & Technology  BBC MMIX
  14. 14. Class Details • More pseudo-code package B::PVIV; use parent qw( B::PV B::IV ); sub B::PV::as_string { $_[0]->{pv} } sub B::IV::as_string { $_[0]->{iv} } • Printing: print $pviv->as_string; # Str print $pviv->B::IV::as_string; # Int Future Media & Technology  BBC MMIX
  15. 15. Real World Pain Future Media & Technology  BBC MMIX
  16. 16. Real World Pain Future Media & Technology  BBC MMIX
  17. 17. Real World Pain Future Media & Technology  BBC MMIX
  18. 18. Real World Pain Future Media & Technology  BBC MMIX
  19. 19. Systems Grow Future Media & Technology  BBC MMIX
  20. 20. The Problem • Responsibility –Wants larger classes • Reuse –Wants smaller classes Future Media & Technology  BBC MMIX
  21. 21. Solution Decouple! Future Media & Technology  BBC MMIX
  22. 22. Solutions • Interfaces –Reimplementing Future Media & Technology  BBC MMIX
  23. 23. Solutions • Delegation –Scaffolding –Communication Future Media & Technology  BBC MMIX
  24. 24. Solutions • Mixins –Ordering Future Media & Technology  BBC MMIX
  25. 25. PracticalJoke • Needs: –explode() –fuse() Future Media & Technology  BBC MMIX
  26. 26. Shared Behavior Method Description ✓ Bomb::fuse() Deterministic Spouse::fuse() Non-deterministic Bomb::explode() Lethal ✓ Spouse::explode() Wish it was lethal Future Media & Technology  BBC MMIX
  27. 27. Ruby Mixins module Bomb def explode puts "Bomb explode" end def fuse puts "Bomb fuse" end end module Spouse def explode puts "Spouse explode" end def fuse puts "Spouse fuse" end end Future Media & Technology  BBC MMIX
  28. 28. Ruby Mixins class PracticalJoke include Spouse include Bomb end joke = PracticalJoke.new() joke.fuse joke.explode Prints out: Future Media & Technology  BBC MMIX
  29. 29. Ruby Mixins class PracticalJoke include Spouse include Bomb end joke = PracticalJoke.new() joke.fuse joke.explode Prints out: Bomb fuse Bomb explode Future Media & Technology  BBC MMIX
  30. 30. Moose Roles { package Bomb; use Moose::Role; sub fuse { say "Bomb explode" } sub explode { say "Bomb fuse" } } { package Spouse; use Moose::Role; sub fuse { say "Spouse explode" } sub explode { say "Spouse fuse" } } Future Media & Technology  BBC MMIX
  31. 31. Moose Roles { package PracticalJoke; use Moose; with qw(Bomb Spouse); } my $joke = PracticalJoke->new(); $joke->fuse(); $joke->explode(); Prints out: Future Media & Technology  BBC MMIX
  32. 32. Moose Roles { package PracticalJoke; use Moose; with qw(Bomb Spouse); } my $joke = PracticalJoke->new(); $joke->fuse(); $joke->explode(); Prints out: Due to a method name conflict in roles 'Bomb' and 'Spouse', the method 'fuse' must be implemented or excluded by 'PracticalJoke' Future Media & Technology  BBC MMIX
  33. 33. Moose Roles { package PracticalJoke; use Moose; with 'Bomb' => { excludes => 'explode' }, 'Spouse' => { excludes => 'fuse' }; } my $joke = PracticalJoke->new(); $joke->fuse(); $joke->explode(); Prints out: Spouse fuse Bomb explode Future Media & Technology  BBC MMIX
  34. 34. Moose Roles package PracticalJoke; use Moose; with 'Bomb' => { excludes => 'explode' }, 'Spouse' => { excludes => 'fuse', alias => { fuse => 'random_fuse' } }; And in your actual code: $joke->fuse(14); # timed fuse # or $joke->random_fuse(); # who knows? Future Media & Technology  BBC MMIX
  35. 35. Role Example package Does::Serialize::YAML use Moose::Role; use YAML::Syck; requires 'as_hash'; sub serialize { my $self = shift; return Dump($self->as_hash); } 1; And in your actual code: package My::Object; use Moose; with 'Does::Serialize::YAML'; Future Media & Technology  BBC MMIX
  36. 36. Back To Work Future Media & Technology  BBC MMIX
  37. 37. Work + Roles Future Media & Technology  BBC MMIX
  38. 38. Work + Roles package Country; use Moose; extends "My::ResultSource"; with qw(DoesStatic DoesAuditing); Future Media & Technology  BBC MMIX
  39. 39. Old BBC ResultSet Classes Future Media & Technology  BBC MMIX
  40. 40. New ResultSet Classes Future Media & Technology  BBC MMIX
  41. 41. Work + Roles package BBC::Programme::Episode; use Moose; extends 'BBC::ResultSet'; with qw( DoesSearch::Broadcasts DoesSearch::Tags DoesSearch::Titles DoesSearch::Promotions DoesIdentifier::Universal ); Future Media & Technology  BBC MMIX
  42. 42. Conclusion • Increases comprehension • Increases safety • Decreases complexity Future Media & Technology  BBC MMIX
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×