Perl Design Patterns
             brian d foy
   Stonehenge Consulting Ser vices
         February 24, 2006
     Grand Rap...
Patterns aren’t a ...
code cookbook
module on CPAN
methodology
goal
religion
Patterns are a ...
abstraction for things we need to do
solutions for common problems
name for a design element
common lan...
Three parts

Context
System of forces
Solution
Nothing’s Free

Actions have reactions
Complexity turns up somewhere else
Choices have consequences
Who’s responsible?

Our application needs config information
But we have several modules
Who loads the data?
How do the oth...
A pattern

Load the configuration from anywhere
But load it only one time
Anyone else gets a reference to it
The order does...
What’s in a name

A couple words instead of sentences
We agree on what the name implies
Others know what we mean
“A rose i...
The Singleton
There is only one configuration
Let’s call it a singleton
... or a highlander
We don’t have an implementation...
An implementation
package My::Config;

my $singleton = undef;

sub new {
   my $class = shift;

  return $singleton if def...
A use
package My::Database;
use My::Config;

my $config = My::Config->new( ... );

package My::Network;
use My::Config;

m...
That isn’t the only way

 The pattern is not a prescription
 It’s an option
 Maybe another pattern works better.
A Meta Class
   Write a meta class that contains all of
   the object parts
   Objects talk to the meta class to
   commun...
Delegates
package My::Controller;

sub new {
   my $class = shift;

   my $self = bless {}, $class;

   # weaken some of t...
Some Perl Modules

Apache::DBI
Netscape::Bookmarks
CGI::Prototype
many things in Class::*, almost
Beware

Patterns are not code
No matter what the Gang of Four say
Class::* is code
Ergo, ...
Perl is Better

People like Patterns because they get code
C++, Java suck at some things (Iterators)
Perl doesn’t suck at ...
Further Reading
The Perl Review (lots of articles (by me))
“Design Patterns Aren’t” by Mark Jason
Dominus
 http:/
      /p...
Upcoming SlideShare
Loading in...5
×

Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)

3,160

Published on

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

No Downloads
Views
Total Views
3,160
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

Design Patterns in Perl (Grand Rapids Perl Mongers, 2006)

  1. 1. Perl Design Patterns brian d foy Stonehenge Consulting Ser vices February 24, 2006 Grand Rapids Perl Mongers Sponsored by PriorityHealth
  2. 2. Patterns aren’t a ... code cookbook module on CPAN methodology goal religion
  3. 3. Patterns are a ... abstraction for things we need to do solutions for common problems name for a design element common language for design discussions way to apply a common design to code relationships
  4. 4. Three parts Context System of forces Solution
  5. 5. Nothing’s Free Actions have reactions Complexity turns up somewhere else Choices have consequences
  6. 6. Who’s responsible? Our application needs config information But we have several modules Who loads the data? How do the other modules get it?
  7. 7. A pattern Load the configuration from anywhere But load it only one time Anyone else gets a reference to it The order doesn’t matter
  8. 8. What’s in a name A couple words instead of sentences We agree on what the name implies Others know what we mean “A rose is a rose is a rose”
  9. 9. The Singleton There is only one configuration Let’s call it a singleton ... or a highlander We don’t have an implementation Just a name with implied design elements
  10. 10. An implementation package My::Config; my $singleton = undef; sub new { my $class = shift; return $singleton if defined $singleton; $singleton = bless {}, $class; }
  11. 11. A use package My::Database; use My::Config; my $config = My::Config->new( ... ); package My::Network; use My::Config; my $config = My::Config->new( ... );
  12. 12. That isn’t the only way The pattern is not a prescription It’s an option Maybe another pattern works better.
  13. 13. A Meta Class Write a meta class that contains all of the object parts Objects talk to the meta class to communicate with the other parts use My::Controller; my $controller = My::Controller->new(...); my $value = $controller->config->get( ... );
  14. 14. Delegates package My::Controller; sub new { my $class = shift; my $self = bless {}, $class; # weaken some of these circular refs @$self{ qw(_config _database _network ) } = ( My::Config->new( controller => $self ), DBI->new( ... ), My::Socket->new( controller => $self ), ); $self; } sub config { $_[0]{_config} }
  15. 15. Some Perl Modules Apache::DBI Netscape::Bookmarks CGI::Prototype many things in Class::*, almost
  16. 16. Beware Patterns are not code No matter what the Gang of Four say Class::* is code Ergo, ...
  17. 17. Perl is Better People like Patterns because they get code C++, Java suck at some things (Iterators) Perl doesn’t suck at the same things
  18. 18. Further Reading The Perl Review (lots of articles (by me)) “Design Patterns Aren’t” by Mark Jason Dominus http:/ /perl.plover.com/yak/design/ Design Patterns - Erich Gamma, et al. (Gang of Four) Perl Design Patterns Wiki http:/ /perldesignpatterns.com/

×