moose design patterns
DESCRIPTION
One key benefit of Moose is the Object-Oriented jargon it brings to the table. With Moose, it's very easy to implement common design patterns, as I present in the keynote. This keynote was part of the Israeli Perl Workshop 2011.TRANSCRIPT
![Page 2: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/2.jpg)
Good Code Bad Code
Tuesday, February 28, 2012
![Page 3: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/3.jpg)
OOP Use Cases
• Write code that other developers will use
• Write code that will survive in an ever changing environment
Tuesday, February 28, 2012
![Page 4: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/4.jpg)
Meet The Moose
Tuesday, February 28, 2012
![Page 5: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/5.jpg)
Moose
• Post Modern Object Oriented Perl
• Consistent OO Framework
• Stable
Tuesday, February 28, 2012
![Page 6: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/6.jpg)
A First Class
package Person;use Moose;
has 'name', is => 'ro', isa => 'Str';has 'age', is => 'rw', isa => 'Int';
package main;use feature ':5.10';
my $p = Person->new(name => "James");say $p->name;
Class Def
Class Use
• A class is just a package
• A method is just a sub
• An attribute is ...We’ll get to that later
Tuesday, February 28, 2012
![Page 7: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/7.jpg)
Object Methods
• A method takes the object (invocant) as its first argument
• That’s why we usemy $self = shift
package Car;use Moose;
has 'speed', is => 'ro';
sub go { my $self = shift; print "Vroom Vroom [speed: ", $self->speed, "]\n";}
package main;my $c = Car->new(speed => 10);$c->go;
Tuesday, February 28, 2012
![Page 8: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/8.jpg)
Whats In The Box
• A new method
• use strict, use warnings
• Type Validation
• Organize Your Code
Tuesday, February 28, 2012
![Page 9: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/9.jpg)
OO Design
Tuesday, February 28, 2012
![Page 10: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/10.jpg)
OO Design Patterns
Tested, Proven development paradigms for speeding up
development process
Tuesday, February 28, 2012
![Page 11: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/11.jpg)
Pattern Structure
• Name
• Problem
• Solution
• Consequences
Tuesday, February 28, 2012
![Page 12: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/12.jpg)
Categories
Creational Behavioral Structural
Singleton
Factory
Builder
Observer
Template Method
Mixins
Composite
Flyweight
Tuesday, February 28, 2012
![Page 13: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/13.jpg)
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
![Page 14: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/14.jpg)
Singleton Pattern
• Ensure a class only has one instance
• Manage Resource Sharing
Tuesday, February 28, 2012
![Page 15: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/15.jpg)
Moose Singletonpackage 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
![Page 16: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/16.jpg)
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
![Page 17: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/17.jpg)
Factory
• Use a Role to specify common behavior
package AppConfig;use Moose::Role;
requires 'debug_mode';requires 'native_separators';requires 'root_fs';
Tuesday, February 28, 2012
![Page 18: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/18.jpg)
Factory
• All creation logic stays in the factory
package ConfigFactory;use Moose;
sub build_config { my $cfg; given ($^O) { $cfg = WinConfig->new when /MSWin32/; $cfg = UnixConfig->new; } return $cfg;}
Tuesday, February 28, 2012
![Page 19: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/19.jpg)
Factory
• Users only need to know about the role, not the various implementations
package main;
my $cfg = ConfigFactory->build_config;
say $cfg->debug_mode;
Tuesday, February 28, 2012
![Page 20: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/20.jpg)
Creational Patterns
Tuesday, February 28, 2012
![Page 21: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/21.jpg)
Behavioral Patterns
• Assignment of responsibility between objects and classes
• Use either inheritance or composition
Tuesday, February 28, 2012
![Page 22: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/22.jpg)
Template Methods
Tuesday, February 28, 2012
![Page 23: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/23.jpg)
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
![Page 24: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/24.jpg)
Painter Example
Draw Pixel
Tuesday, February 28, 2012
![Page 25: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/25.jpg)
Roles: Partials
• Template methods are implemented using roles
• Use requires to define a partial implementation
package Painter;use Moose::Role;
requires 'drawPixel';
sub draw_line { ... }sub draw_triangle { ... }sub draw_rectangle { ... }
Tuesday, February 28, 2012
![Page 26: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/26.jpg)
Roles: Partialspackage 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
![Page 27: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/27.jpg)
Behavioral Patterns
Tuesday, February 28, 2012
![Page 28: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/28.jpg)
Structural
• Control structure of an object
• Is it composed of other objects ?
• How are these parts used ?
• Composition, Decorator, Adapter
Tuesday, February 28, 2012
![Page 29: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/29.jpg)
Composition: What
Contact EmailSend Mail Send Mail
Phone
CallCall
Tuesday, February 28, 2012
![Page 30: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/30.jpg)
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
![Page 31: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/31.jpg)
Delegation: Howpackage Contact;use Moose;
has 'email' => ( is => 'ro', handles => [ qw/send_mail/ ]);
• Can take regular expressions
• Can take hashref
• perldoc Moose::Manual::Delegation my $c = Contact->new;
$c->send_mail(subject => "Hello", text => "...");
Tuesday, February 28, 2012
![Page 32: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/32.jpg)
Delegation
• Delegation is explicit
• Performed via attributes
• Highly recommended
Tuesday, February 28, 2012
![Page 33: Moose Design Patterns](https://reader034.vdocument.in/reader034/viewer/2022052523/5555ce8bd8b42aaf158b4bfd/html5/thumbnails/33.jpg)
OO DesignConsider design patterns
Use the power of perl
Clean Code is worth it
Tuesday, February 28, 2012