I have some transaction line items and I need to generate future billing schedules from those line items. Right now there are four different types of line items, each of which requires slightly different billing schedule generation logic. I expect more types of line items in the future.
Currently I have a BillingScheduleGenerator class that accepts a list of line items, sorts the line items, retrieves required configuration information from the database, and fires off the correct billing schedule generation logic for each group of line items.
The problem is that this class is becoming huge.
I think the best solution is to make a series of subclasses like this:
BillingScheduleGenerator
LineItemBillingScheduleGenerator extends BillingScheduleGenerator
PurchaseLineItemBillingScheduleGenerator extends LineItemBillingScheduleGenerator
CancellationLineItemBillingScheduleGenerator extends LineItemBillingScheduleGenerator
...
This way the more specific line item generators can share the more general line item generation logic (monthly proration, retrieving pricing information ect.) and the specific generation logic that doesn't need to be shared can be encapsulated in a smaller class.
So the question I'm struggling with now is: where do I put the logic that sorts the line items and retrieves the configuration information from the database?
Should it live inside the LineItemBillingScheduleGenerator super class? That would allow callers to instantiate the LineItemBillingScheduleGenerator with a list of line items, call a single method, and receive a list of billing schedules in return.
Is it 'wrong' for a superclass to instantiate and call each of its sub classes?
Should I just put the sorting and querying logic into a service method?