SlideShare a Scribd company logo
1 of 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

More Related Content

More from 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
Ā 

More from 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
Ā 

Recently uploaded

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
Ā 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
Ā 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
Ā 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
Ā 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
Ā 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
Ā 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
Ā 
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 2024The Digital Insurer
Ā 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
Ā 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
Ā 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
Ā 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
Ā 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
Ā 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
Ā 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
Ā 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
Ā 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
Ā 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
Ā 

Recently uploaded (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
Ā 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
Ā 
+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...
Ā 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
Ā 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Ā 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Ā 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
Ā 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Ā 
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
Ā 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Ā 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Ā 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
Ā 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
Ā 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
Ā 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Ā 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
Ā 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
Ā 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
Ā 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Ā 

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