SlideShare une entreprise Scribd logo
1  sur  42
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
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 On Demands
 July 2, 2009



  Future Media & Technology    BBC MMIX
A Brief History of Pain

•Simula 67
 –Classes
 –Polymorphism
 –Encapsulation
 –Inheritance

 Future Media & Technology    BBC MMIX
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 & Technology    BBC MMIX
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
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
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       BBC MMIX
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::fuse()      Non-deterministic


         Bomb::explode()     Lethal



✓        Spouse::explode()   Wish it was lethal


Future Media & Technology                          BBC MMIX
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
Ruby Mixins
    class PracticalJoke
        include Spouse
        include Bomb
    end

    joke = PracticalJoke.new()
    joke.fuse
    joke.explode


Prints out:




  Future Media & Technology        BBC MMIX
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
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
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
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
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
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
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
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 & Technology       BBC MMIX
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(
          DoesSearch::Broadcasts
          DoesSearch::Tags
          DoesSearch::Titles
          DoesSearch::Promotions
          DoesIdentifier::Universal
       );



Future Media & Technology              BBC MMIX
Conclusion

• Increases comprehension
• Increases safety
• Decreases complexity




 Future Media & Technology        BBC MMIX

Contenu connexe

Plus de Curtis Poe

Rescuing a-legacy-codebase
Rescuing a-legacy-codebaseRescuing a-legacy-codebase
Rescuing a-legacy-codebaseCurtis Poe
 
Perl 6 For Mere Mortals
Perl 6 For Mere MortalsPerl 6 For Mere Mortals
Perl 6 For Mere MortalsCurtis Poe
 
Disappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
Disappearing Managers, YAPC::EU 2014, Bulgaria, KeynoteDisappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
Disappearing Managers, YAPC::EU 2014, Bulgaria, KeynoteCurtis Poe
 
How to Fake a Database Design
How to Fake a Database DesignHow to Fake a Database Design
How to Fake a Database DesignCurtis Poe
 
Are Managers An Endangered Species?
Are Managers An Endangered Species?Are Managers An Endangered Species?
Are Managers An Endangered Species?Curtis Poe
 
The Lies We Tell About Software Testing
The Lies We Tell About Software TestingThe Lies We Tell About Software Testing
The Lies We Tell About Software TestingCurtis Poe
 
A/B Testing - What your mother didn't tell you
A/B Testing - What your mother didn't tell youA/B Testing - What your mother didn't tell you
A/B Testing - What your mother didn't tell youCurtis Poe
 
Test::Class::Moose
Test::Class::MooseTest::Class::Moose
Test::Class::MooseCurtis Poe
 
A Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::ClassA Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::ClassCurtis Poe
 
Agile Companies Go P.O.P.
Agile Companies Go P.O.P.Agile Companies Go P.O.P.
Agile Companies Go P.O.P.Curtis Poe
 
Testing With Test::Class
Testing With Test::ClassTesting With Test::Class
Testing With Test::ClassCurtis Poe
 
Logic Progamming in Perl
Logic Progamming in PerlLogic Progamming in Perl
Logic Progamming in PerlCurtis Poe
 
Inheritance Versus Roles - The In-Depth Version
Inheritance Versus Roles - The In-Depth VersionInheritance Versus Roles - The In-Depth Version
Inheritance Versus Roles - The In-Depth VersionCurtis Poe
 
Turbo Charged Test Suites
Turbo Charged Test SuitesTurbo Charged Test Suites
Turbo Charged Test SuitesCurtis Poe
 

Plus de Curtis Poe (15)

Rescuing a-legacy-codebase
Rescuing a-legacy-codebaseRescuing a-legacy-codebase
Rescuing a-legacy-codebase
 
Perl 6 For Mere Mortals
Perl 6 For Mere MortalsPerl 6 For Mere Mortals
Perl 6 For Mere Mortals
 
Disappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
Disappearing Managers, YAPC::EU 2014, Bulgaria, KeynoteDisappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
Disappearing Managers, YAPC::EU 2014, Bulgaria, Keynote
 
How to Fake a Database Design
How to Fake a Database DesignHow to Fake a Database Design
How to Fake a Database Design
 
Are Managers An Endangered Species?
Are Managers An Endangered Species?Are Managers An Endangered Species?
Are Managers An Endangered Species?
 
The Lies We Tell About Software Testing
The Lies We Tell About Software TestingThe Lies We Tell About Software Testing
The Lies We Tell About Software Testing
 
A/B Testing - What your mother didn't tell you
A/B Testing - What your mother didn't tell youA/B Testing - What your mother didn't tell you
A/B Testing - What your mother didn't tell you
 
Test::Class::Moose
Test::Class::MooseTest::Class::Moose
Test::Class::Moose
 
A Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::ClassA Whirlwind Tour of Test::Class
A Whirlwind Tour of Test::Class
 
Agile Companies Go P.O.P.
Agile Companies Go P.O.P.Agile Companies Go P.O.P.
Agile Companies Go P.O.P.
 
Econ101
Econ101Econ101
Econ101
 
Testing With Test::Class
Testing With Test::ClassTesting With Test::Class
Testing With Test::Class
 
Logic Progamming in Perl
Logic Progamming in PerlLogic Progamming in Perl
Logic Progamming in Perl
 
Inheritance Versus Roles - The In-Depth Version
Inheritance Versus Roles - The In-Depth VersionInheritance Versus Roles - The In-Depth Version
Inheritance Versus Roles - The In-Depth Version
 
Turbo Charged Test Suites
Turbo Charged Test SuitesTurbo Charged Test Suites
Turbo Charged Test Suites
 

Dernier

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 

Dernier (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 

Inheritance Versus Roles

  • 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. Not a Tutorial • #ovidfail • “How” is easy • “Why” is not Future Media & Technology  BBC MMIX
  • 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. A Brief History of Pain •Simula 67 –Classes –Polymorphism –Encapsulation –Inheritance Future Media & Technology  BBC MMIX
  • 8. Four Decades of Pain •Code smell –In the language! Future Media & Technology  BBC MMIX
  • 9. B:: Object Hierarchy Future Media & Technology  BBC MMIX
  • 10. A Closer Look Future Media & Technology  BBC MMIX
  • 11. A Closer Look • Multiple Inheritance Future Media & Technology  BBC MMIX
  • 12. B::PVIV Pseudo-Code • B::PVIV Internals bless => { pv => 'three', iv => 3, } => 'B::PVIV'; Future Media & Technology  BBC MMIX
  • 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. 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. Real World Pain Future Media & Technology  BBC MMIX
  • 16. Real World Pain Future Media & Technology  BBC MMIX
  • 17. Real World Pain Future Media & Technology  BBC MMIX
  • 18. Real World Pain Future Media & Technology  BBC MMIX
  • 19. Systems Grow Future Media & Technology  BBC MMIX
  • 20. The Problem • Responsibility –Wants larger classes • Reuse –Wants smaller classes Future Media & Technology  BBC MMIX
  • 21. Solution Decouple! Future Media & Technology  BBC MMIX
  • 22. Solutions • Interfaces –Reimplementing Future Media & Technology  BBC MMIX
  • 23. Solutions • Delegation –Scaffolding –Communication Future Media & Technology  BBC MMIX
  • 24. Solutions • Mixins –Ordering Future Media & Technology  BBC MMIX
  • 25. PracticalJoke • Needs: –explode() –fuse() Future Media & Technology  BBC MMIX
  • 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. 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. Ruby Mixins class PracticalJoke include Spouse include Bomb end joke = PracticalJoke.new() joke.fuse joke.explode Prints out: Future Media & Technology  BBC MMIX
  • 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. 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. 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. 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. 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. 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. 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. Back To Work Future Media & Technology  BBC MMIX
  • 37. Work + Roles Future Media & Technology  BBC MMIX
  • 38. Work + Roles package Country; use Moose; extends "My::ResultSource"; with qw(DoesStatic DoesAuditing); Future Media & Technology  BBC MMIX
  • 39. Old BBC ResultSet Classes Future Media & Technology  BBC MMIX
  • 40. New ResultSet Classes Future Media & Technology  BBC MMIX
  • 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. Conclusion • Increases comprehension • Increases safety • Decreases complexity Future Media & Technology  BBC MMIX