in reply to Re: Rethinking Your Program's If-Thens
in thread Rethinking Your Program's If-Thens

All those if/elsif's make your code look somewhat like 'The Wrong Way' to do it according to the Patterns in Perl web site. I'm not sure how you would fit this problem into something resembling 'The Right Way' though I have an idea. If dcr_type is tainted, you'd have to make sure that it matches '^(CONTACT|FAQ|LITERATURE|ORGANIZATION)$', then assuming that you have those four classes defined, you can just call $dcr_type->new(@args). You could even do $dcr = s/(.*)/\u\L$1/ so you can have normal ucfirst type class names. Keep in mind I haven't really thought much about the OP's problem, so I don't know how appropriate any of this is for this particular case :-)

Replies are listed 'Best First'.
Re: Re: Re: Rethinking Your Program's If-Thens
by Steve_p (Priest) on Oct 12, 2002 at 16:05 UTC
    It would be the wrong way if it were all one class. In this case, the if is in the factory class. In the example, there is one class who's behaviors change based on the values in the class. What I have above is a factory class deciding what subclass to return based on the value passed in. Each class, then, behaves differently.