Let’s say one has a class that performs a certain type of task. And let’s say that there are a number of variations of that task. The actions are the same, just a few parameters change (e.g., for soft boiled egg, action = boil, time = 5 min.; for hard boiled egg, action = boil, time = 11 min., etc.). Number of parameters that vary is about 10.
I see there are three ways to do this:
-
Use a switch and set the params in code based on type.
-
Save the parameters in a database or file and retrieve them based on task type.
-
Subclass the task, overriding the parameters of the parent class and instantiate subclassed objects to perform the task in question.
The first option is clumsy. But how do I decide between the other two?
1) Retrieve parameters from file or db.
- PRO: No need for subclassing or factory. Simple.
- CON: Requires additional query or file access. Parameters no longer visible in code.
2) Subclass the task.
- PRO: Does not require additional query or file access. Parameters maintained in code.
- CON: Proliferation of classes and need to make factory.
Have I correctly identified the pros and cons? What other criteria should I use to decide the issue?
Please advise. THANKS!
My instinct says that if the objects are of logically different classes they need to be of physically different classes.
Option 1 might be acceptable for specific problems, but options 2 is way out. A class should be self-defined without deference to runtime data. (there’s probably even exceptions to that too, but I like hyperbole)