oo systems and roles
DESCRIPTION
OO Systems and Roles. Curtis "Ovid" Poe http:// blogs.perl.org/users/ovid /. Not A Tutorial. "How" is easy "Why" is not. One of These Things Is Not Like The Others. Simula 67 Classes Polymorphism Encapsulation Inheritance. Multiple Inheritance. Perl C++ Eiffel CLOS Python. - PowerPoint PPT PresentationTRANSCRIPT
![Page 3: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/3.jpg)
One of These Things Is Not Like The Others
• Simula 67– Classes– Polymorphism– Encapsulation– Inheritance
![Page 12: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/12.jpg)
B::PVIV Pseudo-Code• B::PVIV Internals
bless { pv => 'three', # usually '3' iv => 3,} => 'B::PVIV';
![Page 13: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/13.jpg)
Printing NumbersPerlmy $number = 3;$number += 2;
# << fits on slide say <<"END";I have $number applesEND
Javaint number = 3;number += 2;System.out.println( "I have " + number + " apples");
![Page 14: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/14.jpg)
More Pseudo-codesub B::PV::as_string { shift->pv } sub B::IV::as_string { shift->iv }
package B::PVIV;use parent qw( B::PV B::IV );
# latersay $pviv->as_string; # Strsay $pviv->B::IV::as_string; # Int
![Page 15: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/15.jpg)
Systems Grow
Credit: Kishorekumar 62 http://en.wikipedia.org/wiki/File:UML_Diagrams.jpg
![Page 16: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/16.jpg)
The Real Problem• Responsibility–Wants larger classes
Versus
• Reuse–Wants smaller classes
![Page 20: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/20.jpg)
Code Reuse
Method Description
✓ Bomb::fuse() Deterministic
Spouse::fuse() Non-deterministic
Bomb::explode() Lethal
✓ Spouse::explode() Wish it was lethal
![Page 21: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/21.jpg)
Ruby Mixinsmodule Bomb def explode puts "Bomb explode" end def fuse puts "Bomb fuse" endend
module Spouse def explode puts "Spouse explode" end def fuse puts "Spouse fuse" endend
![Page 22: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/22.jpg)
Ruby Mixinsclass PracticalJoke include Spouse include Bombend
joke = PracticalJoke.new()joke.fusejoke.explode
![Page 24: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/24.jpg)
Ruby MixinsBomb fuseBomb explode
irb(main):026:0> PracticalJoke.ancestors=> [PracticalJoke, Bomb, Spouse, Object, Kernel]
![Page 25: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/25.jpg)
Moose Rolespackage 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";}
![Page 26: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/26.jpg)
Moose Roles{ package PracticalJoke; use Moose; with qw(Bomb Spouse);}my $joke = PracticalJoke->new; $joke->fuse;$joke->explode;
![Page 27: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/27.jpg)
Moose RolesDue to method name conflicts in roles'Bomb' and 'Spouse', the methods 'explode'and 'fuse' must be implemented or excluded by 'PracticalJoke'… plus
… the… stack
… trace… from
… hell
![Page 28: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/28.jpg)
Moose Roles{ package PracticalJoke; use Moose; with 'Bomb' => { excludes => 'explode' }, 'Spouse' => { excludes => 'fuse' };}my $joke = PracticalJoke->new;$joke->fuse;$joke->explode;# Bomb fuse # Spouse explode
![Page 29: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/29.jpg)
Moose Roles{ package PracticalJoke; use Moose; with 'Bomb' => { excludes => 'explode' }, 'Spouse' => { excludes => 'fuse', alias => { fuse => 'random_fuse' }};}
my $joke = PracticalJoke->new;$joke->random_fuse;
![Page 30: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/30.jpg)
Moose RolesClasspackage My::Object;use Moose;with 'Does::AsYAML';
sub to_hash {…
}
Rolepackage Does::AsYAML;use Moose::Role;use YAML::Syck;
requires qw(to_hash);
sub to_yaml { my $self = shift; return Dump( $self->to_hash );} 1;
![Page 31: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/31.jpg)
Languages With Roles (traits)
• Xerox "Star" (the origin of traits in '79/'80)• Self• Perl 6• Perl 5 (via Moose and others)• Smalltalk (Pharo)• Fortress• Scala• Javascript (via Joose)• PHP (5.4 and above)• Slate
![Page 32: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/32.jpg)
The Problem Domain• 5,613 brands• 6,755 series• 386,943 episodes• 394,540 versions• 1,106,246 broadcasts• … and growing rapidly
![Page 38: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/38.jpg)
Switching to Rolespackage Country;use Moose;extends "My::ResultSource";with qw( DoesStatic DoesAuditing);
![Page 41: OO Systems and Roles](https://reader036.vdocuments.site/reader036/viewer/2022062410/56816093550346895dcfb8ac/html5/thumbnails/41.jpg)
Increased Comprehensionpackage BBC::Programme::Episode;use Moose;extends 'BBC::ResultSet';with qw( Does::Search::ForBroadcast Does::Search::ByTag Does::Search::ByTitle Does::Search::ByPromotion Does::Identifier::Universal);