Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How to implement a Design Pattern/ Strategy??

by Anonymous Monk
on Jun 12, 2011 at 14:57 UTC ( [id://909304]=note: print w/replies, xml ) Need Help??


in reply to How to implement a Design Pattern/ Strategy??

Well, I haven't looked too closely, but from the short if/else section, you want to start by making functions, like this
if ($category_id eq "all") { push @all_matches, SomethingAll(...); } elsif ($category_id eq "subj") { push @all_matches, SomethingSubj(...); }
which you would later turn into a dispatch table
my %dispatch_table = ( all => \&SomethingAll subj => \&SomethingSubj _default => \&SomethingDefault ); ... my $callback = $dispatch_table{$category_id} || $dispatch_table{_defau +lt}; push @all_matches, $callback->(...);
OO not required. When deciding which function to call requires something more complicated than a simple string comparison (key lookup), you'll benefit more from using object-oriented-programming.

perl Moose Strategy Design Pattern -> https://secure.wikimedia.org/wikibooks/en/wiki/Computer_Science_Design_Patterns/Strategy#Perl

Replies are listed 'Best First'.
Re^2: How to implement a Design Pattern/ Strategy??
by jonc (Beadle) on Jun 12, 2011 at 17:57 UTC

    Thanks a lot for breaking it down. You make it sound like I could almost do it. I will try implementing your steps.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://909304]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found