Moose Design                               Patterns                Ynon Perek                ynonperek@yahoo.com          ...
Good Code   Bad CodeTuesday, February 28, 2012
OOP Use Cases                    • Write code that other developers                             will use                  ...
Meet The MooseTuesday, February 28, 2012
Moose                    • Post Modern Object Oriented Perl                    • Consistent OO Framework                  ...
A First Class                                                                                Class Def                    ...
Object Methods           package Car;           use Moose;           has speed, is => ro;              •   A method takes ...
Whats In The Box            •      A new method            •      use strict, use warnings            •      Type Validati...
OO DesignTuesday, February 28, 2012
OO Design                   Patterns               Tested, Proven development                paradigms for speeding up    ...
Pattern Structure                    • Name                    • Problem                    • Solution                    ...
Categories           Creational          Behavioral    Structural            Singleton         Observer      Mixins       ...
Creational Patterns                    • Abstract instantiation process                    • We must only create one log fi...
Singleton Pattern                    • Ensure a class only has one instance                    • Manage Resource SharingTu...
Moose Singleton           package Logger;           use MooseX::Singleton;           sub debug { ... }           sub warn ...
Factory                    • Create a different object based                             on some conditional              ...
Factory           package AppConfig;           use Moose::Role;           requires debug_mode;          •   Use a Role to ...
Factory     package ConfigFactory;     use Moose;     sub build_config {         my $cfg;                                 ...
Factory         package main;                            •   Users only need                                              ...
Creational PatternsTuesday, February 28, 2012
Behavioral Patterns                    • Assignment of responsibility                             between objects and clas...
Template MethodsTuesday, February 28, 2012
Template Method                    • Separate the algorithm from the actual                             implementation    ...
Painter Example                                Draw PixelTuesday, February 28, 2012
Roles: Partials                                                       package Painter;                    •        Templat...
Roles: Partials                  package ScreenPainter;                  use Moose;                  with Painter;        ...
Behavioral PatternsTuesday, February 28, 2012
Structural                    • Control structure of an object                    • Is it composed of other objects ?     ...
Composition: What                 Send Mail                  Send Mail                                  Contact           ...
Moose Composition                    • Moose has a built-in support for delegation                    • Use handles on an ...
Delegation: How                                                    package Contact;                  •     Can take regula...
Delegation                    • Delegation is explicit                    • Performed via attributes                    • ...
OO Design           Consider design patterns           Use the power of perl           Clean Code is worth itTuesday, Febr...
Thanks For                              Listening                Ynon Perek                ynonperek@yahoo.com            ...
Upcoming SlideShare
Loading in …5
×

Moose Design Patterns

2,719 views

Published on

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.

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

No Downloads
Views
Total views
2,719
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
31
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Moose Design Patterns

  1. 1. Moose Design Patterns Ynon Perek ynonperek@yahoo.com http://ynonperek.comTuesday, February 28, 2012
  2. 2. Good Code Bad CodeTuesday, February 28, 2012
  3. 3. OOP Use Cases • Write code that other developers will use • Write code that will survive in an ever changing environmentTuesday, February 28, 2012
  4. 4. Meet The MooseTuesday, February 28, 2012
  5. 5. Moose • Post Modern Object Oriented Perl • Consistent OO Framework • StableTuesday, February 28, 2012
  6. 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. 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. 8. Whats In The Box • A new method • use strict, use warnings • Type Validation • Organize Your CodeTuesday, February 28, 2012
  9. 9. OO DesignTuesday, February 28, 2012
  10. 10. OO Design Patterns Tested, Proven development paradigms for speeding up development processTuesday, February 28, 2012
  11. 11. Pattern Structure • Name • Problem • Solution • ConsequencesTuesday, February 28, 2012
  12. 12. Categories Creational Behavioral Structural Singleton Observer Mixins Template Factory Method Composite Builder FlyweightTuesday, February 28, 2012
  13. 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-nodeTuesday, February 28, 2012
  14. 14. Singleton Pattern • Ensure a class only has one instance • Manage Resource SharingTuesday, February 28, 2012
  15. 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. 16. Factory • Create a different object based on some conditional • Treat the newly created objects the same way • Practical: abstract away OS related codeTuesday, February 28, 2012
  17. 17. Factory package AppConfig; use Moose::Role; requires debug_mode; • Use a Role to requires native_separators; specify common requires root_fs; behaviorTuesday, February 28, 2012
  18. 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. 19. Factory package main; • Users only need to know about my $cfg = ConfigFactory->build_config; the role, not the say $cfg->debug_mode; various implementationsTuesday, February 28, 2012
  20. 20. Creational PatternsTuesday, February 28, 2012
  21. 21. Behavioral Patterns • Assignment of responsibility between objects and classes • Use either inheritance or compositionTuesday, February 28, 2012
  22. 22. Template MethodsTuesday, February 28, 2012
  23. 23. Template Method • Separate the algorithm from the actual implementation • Define the skeleton of an algorithm • Example: Paint on a canvas or printerTuesday, February 28, 2012
  24. 24. Painter Example Draw PixelTuesday, February 28, 2012
  25. 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. 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. 27. Behavioral PatternsTuesday, February 28, 2012
  28. 28. Structural • Control structure of an object • Is it composed of other objects ? • How are these parts used ? • Composition, Decorator, AdapterTuesday, February 28, 2012
  29. 29. Composition: What Send Mail Send Mail Contact Email Call Call PhoneTuesday, February 28, 2012
  30. 30. Moose Composition • Moose has a built-in support for delegation • Use handles on an attribute to create an effective composition • Prefer composition over inheritanceTuesday, February 28, 2012
  31. 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. 32. Delegation • Delegation is explicit • Performed via attributes • Highly recommendedTuesday, February 28, 2012
  33. 33. OO Design Consider design patterns Use the power of perl Clean Code is worth itTuesday, February 28, 2012
  34. 34. Thanks For Listening Ynon Perek ynonperek@yahoo.com http://ynonperek.comTuesday, February 28, 2012

×