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.
In reply to replace conditionals with polymorphism by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |