in reply to OO: Building an object of the right type based on a parameter
Seems to that you need an Invoice-type field in your table. Then when you instantiate an instance of your InvoiceCalculator passing the Client object, it (the IC constructor) can query the invoice-type from the passed instance and bless the instance into the correct subclass. Concatenating the Invoice-type (say 'A', 'B', 'C' etc.) with it's classname to form the subclass name to bless into.
package InvoiceCalculator; sub new{ my( $class, $client ) = @_; my $InvoiceType = $client->InvoiceType; my $self = ....; return bless $self, "$class::$InvoiceType"; }
This way, adding new invoice type just requires you to create the appropriate subclass of InvoiceCalculator, and changing the type of a clients invoice just requires updating the appropriate field in that clients record in the table. You get complete flexibility and avoid hardcoded if/else cascades, and minimise code changes.
|
|---|