Moose Design
                               Patterns
                Ynon Perek
                ynonperek@yahoo.com
                http://ynonperek.com




Tuesday, February 28, 2012
Good Code   Bad Code




Tuesday, February 28, 2012
OOP Use Cases

                    • Write code that other developers
                             will use
                    • Write code that will survive in an ever
                             changing environment




Tuesday, February 28, 2012
Meet The Moose
Tuesday, February 28, 2012
Moose

                    • Post Modern Object Oriented Perl
                    • Consistent OO Framework
                    • Stable


Tuesday, February 28, 2012
A First Class
                                                                                Class Def
                                                         package Person;


                    •
                                                         use Moose;
                             A class is just a package
                                                         has 'name', is => 'ro', isa => 'Str';
                                                         has 'age', is => 'rw', isa => 'Int';

                    •        A method is just a sub

                    •        An attribute is ...
                                                                                Class Use
                             We’ll get to that later     package main;
                                                         use feature ':5.10';

                                                         my $p = Person->new(name => "James");
                                                         say $p->name;




Tuesday, February 28, 2012
Object Methods
           package Car;
           use Moose;

           has 'speed', is => 'ro';              •   A method takes
                                                     the object
           sub go {
               my $self = shift;                     (invocant) as its
               print   "Vroom Vroom [speed: ",       first argument
                       $self->speed,
                       "]n";
           }
                                                 •   That’s why we use
           package main;                             my $self = shift
           my $c = Car->new(speed => 10);
           $c->go;




Tuesday, February 28, 2012
Whats In The Box


            •      A new method

            •      use strict, use warnings

            •      Type Validation

            •      Organize Your Code




Tuesday, February 28, 2012
OO Design
Tuesday, February 28, 2012
OO Design
                   Patterns
               Tested, Proven development
                paradigms for speeding up
                  development process




Tuesday, February 28, 2012
Pattern Structure

                    • Name
                    • Problem
                    • Solution
                    • Consequences

Tuesday, February 28, 2012
Categories
           Creational          Behavioral    Structural

            Singleton         Observer      Mixins
                              Template
            Factory
                              Method        Composite
             Builder
                               Flyweight



Tuesday, February 28, 2012
Creational Patterns
                    • Abstract instantiation process
                    • We must only create one log file instance
                             for the entire system
                    • An XML tree is built gradually,
                             node-by-node




Tuesday, February 28, 2012
Singleton Pattern

                    • Ensure a class only has one instance
                    • Manage Resource Sharing


Tuesday, February 28, 2012
Moose Singleton
           package Logger;
           use MooseX::Singleton;

           sub debug { ... }
           sub warn { ... }

           package main;

           my $logger         = Logger->instance;
           my $same           = Logger->instance;

           my $and_again      = Logger->new;

           $logger->debug("Hello World");




Tuesday, February 28, 2012
Factory
                    • Create a different object based
                             on some conditional
                    • Treat the newly created objects
                             the same way
                    • Practical: abstract away OS related code

Tuesday, February 28, 2012
Factory
           package AppConfig;
           use Moose::Role;

           requires 'debug_mode';          •   Use a Role to
           requires 'native_separators';
                                               specify common
           requires 'root_fs';                 behavior




Tuesday, February 28, 2012
Factory
     package ConfigFactory;
     use Moose;

     sub build_config {
         my $cfg;                                    •   All creation logic
         given ($^O) {                                   stays in the
             $cfg = WinConfig->new when /MSWin32/;
             $cfg = UnixConfig->new;
                                                         factory
         }
         return $cfg;
     }




Tuesday, February 28, 2012
Factory

         package main;                            •   Users only need
                                                      to know about
         my $cfg = ConfigFactory->build_config;
                                                      the role, not the
         say $cfg->debug_mode;                        various
                                                      implementations




Tuesday, February 28, 2012
Creational Patterns




Tuesday, February 28, 2012
Behavioral Patterns

                    • Assignment of responsibility
                             between objects and classes
                    • Use either inheritance or composition


Tuesday, February 28, 2012
Template Methods
Tuesday, February 28, 2012
Template Method

                    • Separate the algorithm from the actual
                             implementation
                    • Define the skeleton of an algorithm
                    • Example: Paint on a canvas or printer


Tuesday, February 28, 2012
Painter Example
                                Draw Pixel




Tuesday, February 28, 2012
Roles: Partials
                                                       package Painter;

                    •        Template methods are      use Moose::Role;

                             implemented using roles   requires 'drawPixel';


                    •        Use requires to define a   sub draw_line      { ... }
                                                       sub draw_triangle { ... }
                             partial implementation    sub draw_rectangle { ... }




Tuesday, February 28, 2012
Roles: Partials
                  package ScreenPainter;
                  use Moose;

                  with 'Painter';

                  sub draw_pixel { ... }

                  package main;
                  my $painter = ScreenPainter->new;

                  $painter->draw_line(0, 0, 100, 100);




Tuesday, February 28, 2012
Behavioral Patterns




Tuesday, February 28, 2012
Structural

                    • Control structure of an object
                    • Is it composed of other objects ?
                    • How are these parts used ?
                    • Composition, Decorator, Adapter

Tuesday, February 28, 2012
Composition: What

                 Send Mail                  Send Mail
                                  Contact               Email
               Call
                                                 Call

                                                        Phone




Tuesday, February 28, 2012
Moose Composition

                    • Moose has a built-in support for delegation
                    • Use handles on an attribute to create an
                             effective composition
                    • Prefer composition over inheritance


Tuesday, February 28, 2012
Delegation: How
                                                    package Contact;

                  •     Can take regular            use Moose;

                        expressions                 has 'email' => (
                                                        is      => 'ro',
                  •     Can take hashref                handles => [ qw/send_mail/ ]
                                                    );

                  •     perldoc
                        Moose::Manual::Delegation   my $c = Contact->new;
                                                    $c->send_mail(subject => "Hello",
                                                                  text => "...");




Tuesday, February 28, 2012
Delegation

                    • Delegation is explicit
                    • Performed via attributes
                    • Highly recommended


Tuesday, February 28, 2012
OO Design
           Consider design patterns

           Use the power of perl

           Clean Code is worth it




Tuesday, February 28, 2012
Thanks For
                              Listening
                Ynon Perek
                ynonperek@yahoo.com
                http://ynonperek.com




Tuesday, February 28, 2012

Moose Design Patterns

  • 1.
    Moose Design Patterns Ynon Perek ynonperek@yahoo.com http://ynonperek.com Tuesday, February 28, 2012
  • 2.
    Good Code Bad Code Tuesday, February 28, 2012
  • 3.
    OOP Use Cases • Write code that other developers will use • Write code that will survive in an ever changing environment Tuesday, February 28, 2012
  • 4.
    Meet The Moose Tuesday,February 28, 2012
  • 5.
    Moose • Post Modern Object Oriented Perl • Consistent OO Framework • Stable Tuesday, February 28, 2012
  • 6.
    A First Class Class Def package Person; • use Moose; A class is just a package has 'name', is => 'ro', isa => 'Str'; has 'age', is => 'rw', isa => 'Int'; • A method is just a sub • An attribute is ... Class Use We’ll get to that later package main; use feature ':5.10'; my $p = Person->new(name => "James"); say $p->name; Tuesday, February 28, 2012
  • 7.
    Object Methods package Car; use Moose; has 'speed', is => 'ro'; • A method takes the object sub go {     my $self = shift; (invocant) as its     print "Vroom Vroom [speed: ", first argument             $self->speed,             "]n"; } • That’s why we use package main; my $self = shift my $c = Car->new(speed => 10); $c->go; Tuesday, February 28, 2012
  • 8.
    Whats In TheBox • A new method • use strict, use warnings • Type Validation • Organize Your Code Tuesday, February 28, 2012
  • 9.
  • 10.
    OO Design Patterns Tested, Proven development paradigms for speeding up development process Tuesday, February 28, 2012
  • 11.
    Pattern Structure • Name • Problem • Solution • Consequences Tuesday, February 28, 2012
  • 12.
    Categories Creational Behavioral Structural Singleton Observer Mixins Template Factory Method Composite Builder Flyweight Tuesday, February 28, 2012
  • 13.
    Creational Patterns • Abstract instantiation process • We must only create one log file instance for the entire system • An XML tree is built gradually, node-by-node Tuesday, February 28, 2012
  • 14.
    Singleton Pattern • Ensure a class only has one instance • Manage Resource Sharing Tuesday, February 28, 2012
  • 15.
    Moose Singleton package Logger; use MooseX::Singleton; sub debug { ... } sub warn { ... } package main; my $logger = Logger->instance; my $same = Logger->instance; my $and_again = Logger->new; $logger->debug("Hello World"); Tuesday, February 28, 2012
  • 16.
    Factory • Create a different object based on some conditional • Treat the newly created objects the same way • Practical: abstract away OS related code Tuesday, February 28, 2012
  • 17.
    Factory package AppConfig; use Moose::Role; requires 'debug_mode'; • Use a Role to requires 'native_separators'; specify common requires 'root_fs'; behavior Tuesday, February 28, 2012
  • 18.
    Factory package ConfigFactory; use Moose; sub build_config {     my $cfg; • All creation logic     given ($^O) { stays in the         $cfg = WinConfig->new when /MSWin32/;         $cfg = UnixConfig->new; factory     }     return $cfg; } Tuesday, February 28, 2012
  • 19.
    Factory package main; • Users only need to know about my $cfg = ConfigFactory->build_config; the role, not the say $cfg->debug_mode; various implementations Tuesday, February 28, 2012
  • 20.
  • 21.
    Behavioral Patterns • Assignment of responsibility between objects and classes • Use either inheritance or composition Tuesday, February 28, 2012
  • 22.
  • 23.
    Template Method • Separate the algorithm from the actual implementation • Define the skeleton of an algorithm • Example: Paint on a canvas or printer Tuesday, February 28, 2012
  • 24.
    Painter Example Draw Pixel Tuesday, February 28, 2012
  • 25.
    Roles: Partials package Painter; • Template methods are use Moose::Role; implemented using roles requires 'drawPixel'; • Use requires to define a sub draw_line { ... } sub draw_triangle { ... } partial implementation sub draw_rectangle { ... } Tuesday, February 28, 2012
  • 26.
    Roles: Partials package ScreenPainter; use Moose; with 'Painter'; sub draw_pixel { ... } package main; my $painter = ScreenPainter->new; $painter->draw_line(0, 0, 100, 100); Tuesday, February 28, 2012
  • 27.
  • 28.
    Structural • Control structure of an object • Is it composed of other objects ? • How are these parts used ? • Composition, Decorator, Adapter Tuesday, February 28, 2012
  • 29.
    Composition: What Send Mail Send Mail Contact Email Call Call Phone Tuesday, February 28, 2012
  • 30.
    Moose Composition • Moose has a built-in support for delegation • Use handles on an attribute to create an effective composition • Prefer composition over inheritance Tuesday, February 28, 2012
  • 31.
    Delegation: How package Contact; • Can take regular use Moose; expressions has 'email' => (     is => 'ro', • Can take hashref     handles => [ qw/send_mail/ ] ); • perldoc Moose::Manual::Delegation my $c = Contact->new; $c->send_mail(subject => "Hello",               text => "..."); Tuesday, February 28, 2012
  • 32.
    Delegation • Delegation is explicit • Performed via attributes • Highly recommended Tuesday, February 28, 2012
  • 33.
    OO Design Consider design patterns Use the power of perl Clean Code is worth it Tuesday, February 28, 2012
  • 34.
    Thanks For Listening Ynon Perek ynonperek@yahoo.com http://ynonperek.com Tuesday, February 28, 2012