use Moose;Finding Object Oriented Enlightenment
Images Courtesy of   Shutterstock   http://www.shutterstock.com/
ShutterstockDeveloper Ethos
Get feedback as early as possible, and work together
Prefer encapsulated, loosely coupled   systems for core functionality
Choose the smallest implementation that doesn’t         suck provides a way forward
Make your code readable,understandable, maintainable
• Get feedback as early as possible, and work  together• Prefer encapsulated, loosely coupled  systems for core functional...
...the object-oriented approach encourages the programmer to place       data where it is not directly       accessible by...
A Person Walks and Talks
start_walking               stop_walking                Person            speak($something)
Prefer encapsulated, loosely coupledsystems for core functionalityChoose the smallest implementation thatprovides a way fo...
Ash Nazg Durbatulûk! Ash Nazg Gimbatul! Ash Nazg Gimbatul! Ash Nazg Gimbatul!
"I will take the Ring to Mordor. Though — Ido not know the way."    -- Frodo, LoTR
Moose and Perl
Moose is a postmodern objectsystem for Perl 5 that takes the tedium out of writing object-oriented Perl. Itborrows all the...
package Point;use Moose; has x => (isa => Int, is => rw, required => 1);has y => (isa => Int, is => rw, required => 1); su...
Building Blocks•   Inheritance•   Roles•   Delegation
Inheritance
package Point;use Moose; has x => (isa => Int, is => rw, required => 1);has y => (isa => Int, is => rw, required => 1); su...
Roles
package Eq;use Moose::Role;requires equal;my $defensive_coderef = sub { ... }; sub no_equal {    my ($self, $other) = @_; ...
Delegation
package MyObject;use Moose;use MyLogger;has ‘logger’ => (   is => ‘bare’,   lazy_build => 1,   handles => [qw/err warn/],)...
package MyObject;use Moose;use MooseX::Types::LoadableClass LoadableClass;has logger_class => (  is => ro,  default => MyL...
package MyObject;use Moose; with MooseX::Role::BuildInstanceOf => {   target => Logger};has ‘+logger’ => (handles => [qw/e...
Examples
Test::DBIx::Class• Automatically deploy temporary databases  based on exist DBIx::Class schema• Pluggable database targets...
Approach• Create a Base Class with default  functionality• Use Roles to allow ‘plugging in’ various  target temporary data...
DBIx::Class::Migration• Make it easy to create deploys and  upgrades for database schemas• Be flexible but have sane, commu...
Approach• Create a Base class with lots of delegate  placeholders.• Create defaults classes that provide  required functio...
GeneralAssembly::Guestbook • A Test Application to Demonstrate Perl to   inspired learners • Code is probably overkill for...
My Learnings• Delegation seems the most normally useful  approach to creating easily testable building  blocks.• Roles are...
Summary• There is no necessary tension between  writing the least code and writing good  code• Good design lets you evolve...
Moose
Moose
Moose
Moose
Moose
Moose
Moose
Moose
Moose
Upcoming SlideShare
Loading in...5
×

Moose

3,026

Published on

Slides of the presentation I gave for Perl NYC Meetup 23 April 2012 (http://www.meetup.com/The-New-York-Perl-Meetup-Group/events/59990662/)

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

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

No notes for slide
  • \n
  • \n
  • \n
  • great ideas are simple answers to common problems\n\n
  • maybe not so simple in the details!\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • great ideas are simple answers to common problems\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Moose

    1. 1. use Moose;Finding Object Oriented Enlightenment
    2. 2. Images Courtesy of Shutterstock http://www.shutterstock.com/
    3. 3. ShutterstockDeveloper Ethos
    4. 4. Get feedback as early as possible, and work together
    5. 5. Prefer encapsulated, loosely coupled systems for core functionality
    6. 6. Choose the smallest implementation that doesn’t suck provides a way forward
    7. 7. Make your code readable,understandable, maintainable
    8. 8. • Get feedback as early as possible, and work together• Prefer encapsulated, loosely coupled systems for core functionality• Choose the smallest implementation that provides a way forward• Make your code readable, understandable, and maintainable
    9. 9. ...the object-oriented approach encourages the programmer to place data where it is not directly accessible by the rest of the program. Instead, the data is accessed by calling specially written functions, commonly called methods...http://en.wikipedia.org/wiki/Object-oriented_programming
    10. 10. A Person Walks and Talks
    11. 11. start_walking stop_walking Person speak($something)
    12. 12. Prefer encapsulated, loosely coupledsystems for core functionalityChoose the smallest implementation thatprovides a way forwardMake your code readable, understandable,and maintainable
    13. 13. Ash Nazg Durbatulûk! Ash Nazg Gimbatul! Ash Nazg Gimbatul! Ash Nazg Gimbatul!
    14. 14. "I will take the Ring to Mordor. Though — Ido not know the way." -- Frodo, LoTR
    15. 15. Moose and Perl
    16. 16. Moose is a postmodern objectsystem for Perl 5 that takes the tedium out of writing object-oriented Perl. Itborrows all the best features from Perl 6, CLOS (Lisp), Smalltalk, Java, BETA, OCaml, Ruby and more, while still keeping true to its Perl 5 roots. http://moose.iinteractive.com/
    17. 17. package Point;use Moose; has x => (isa => Int, is => rw, required => 1);has y => (isa => Int, is => rw, required => 1); sub clear { my $self = shift;  $self->x(0);  $self->y(0);}
    18. 18. Building Blocks• Inheritance• Roles• Delegation
    19. 19. Inheritance
    20. 20. package Point;use Moose; has x => (isa => Int, is => rw, required => 1);has y => (isa => Int, is => rw, required => 1); sub clear { my $self = shift;  $self->x(0);  $self->y(0);}package Point3D;use Moose; extends Point; has z => (isa => Int, is => rw, required => 1); after clear => sub {    my $self = shift;    $self->z(0);};
    21. 21. Roles
    22. 22. package Eq;use Moose::Role;requires equal;my $defensive_coderef = sub { ... }; sub no_equal {    my ($self, $other) = @_;    !$self->equal($other);} package Currency;use Moose;with Eq; sub equal {    my ($self, $other) = @_;    $self->as_float == $other->as_float;}
    23. 23. Delegation
    24. 24. package MyObject;use Moose;use MyLogger;has ‘logger’ => ( is => ‘bare’, lazy_build => 1, handles => [qw/err warn/],);sub _build_logger { MyLogger->new }sub something_worth_logging { my ($self) = @_; $self->err(“Hey, there’s a problem!!!”);}
    25. 25. package MyObject;use Moose;use MooseX::Types::LoadableClass LoadableClass;has logger_class => (  is => ro,  default => MyLogger,  isa => LoadableClass,  coerce => 1);has logger_args => ( is=>’ro’, isa=>‘HashRef’, default=> sub { +{} });has ‘logger’ => ( is => ‘bare’, lazy_build => 1, handles => [qw/err warn/]);sub _build_logger { my $self = shift; $self->logger_class($self->logger_args) ->new}
    26. 26. package MyObject;use Moose; with MooseX::Role::BuildInstanceOf => { target => Logger};has ‘+logger’ => (handles => [qw/err warn/]);
    27. 27. Examples
    28. 28. Test::DBIx::Class• Automatically deploy temporary databases based on exist DBIx::Class schema• Pluggable database targets (Sqlite, MySQL, PostgreSQL)
    29. 29. Approach• Create a Base Class with default functionality• Use Roles to allow ‘plugging in’ various target temporary database types
    30. 30. DBIx::Class::Migration• Make it easy to create deploys and upgrades for database schemas• Be flexible but have sane, community accepted standards
    31. 31. Approach• Create a Base class with lots of delegate placeholders.• Create defaults classes that provide required functionality
    32. 32. GeneralAssembly::Guestbook • A Test Application to Demonstrate Perl to inspired learners • Code is probably overkill for the actual problem
    33. 33. My Learnings• Delegation seems the most normally useful approach to creating easily testable building blocks.• Roles are great for enforcing contracts and for enhancing objects with cross cutting functionality.• (Combine the two)++
    34. 34. Summary• There is no necessary tension between writing the least code and writing good code• Good design lets you evolve over time without nasty hacks

    ×