Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I don't get how the topic applies to my Perl program. It is basically one big switch.
$foo = indirect user input; given ($foo) { when (A) { a; } when (B) { b; } when (C) { b; one additional statement; } when (D) { d; } default { similar_to_a; } }
Following the advice in the video, I converted the different blocks into subroutines with the same name under differently named subclasses.
However, I have no idea what to write in the base class. There are no examples. I expected something on the design patterns wiki under the lemma ConditionalElimination at slowass.net, but it's down.
First, I need an abstract method. Is it just sub foo {}; or sub foo; or what? Or should I leave the declaration away altogether?
Second, but most important, now that the switch is gone, how do I go about from user input $foo to a certain subclass foo method? Drawing a blank here.
Third, sharing code between B and C is more difficult. Formerly, when everything was in one package, I just had a sub for the common code. How do I handle that now that they are in different classes?
I'm not convinced that once I solve the problems, the conversion is going to be a big win, but I want to learn it anyway for the future. Please answer accordingly.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: replace conditionals with polymorphism
by Corion (Patriarch) on Feb 09, 2009 at 17:00 UTC | |
Re: replace conditionals with polymorphism
by juster (Friar) on Feb 09, 2009 at 17:20 UTC | |
Re: replace conditionals with polymorphism
by ELISHEVA (Prior) on Feb 09, 2009 at 21:01 UTC | |
by doom (Deacon) on Feb 09, 2009 at 23:24 UTC | |
by tilly (Archbishop) on Feb 10, 2009 at 03:01 UTC | |
by doom (Deacon) on Feb 10, 2009 at 04:24 UTC | |
by tilly (Archbishop) on Feb 10, 2009 at 05:58 UTC | |
| |
Re: replace conditionals with polymorphism
by revdiablo (Prior) on Feb 09, 2009 at 16:55 UTC | |
Re: replace conditionals with polymorphism
by gnosti (Chaplain) on Feb 09, 2009 at 17:44 UTC | |
by chromatic (Archbishop) on Feb 09, 2009 at 20:01 UTC | |
by Corion (Patriarch) on Feb 10, 2009 at 08:29 UTC |