How to solve the "Growing If Statement" problem?
- by Achilles
I've been doing some reading about design patterns and wanted some perspective. Consider the following:
Dim objGruntWorker as IGruntWorker
if SomeCriteria then
objGruntWorker = new GoFor()
else if SomeOtherCriteria then
objGruntWorker = new Newb()
else if SomeCriteriaAndTheKitchenSink then
objGruntWorker = new CubeRat()
end if
objGruntWorker.GetBreakfast()
system.threading.thread.sleep(GetMilliSecondsFromHours(4))
objGruntWorker.GetLunch()
The above code grows each time a new Criteria arises. I've seen code like this all over the place and in ignorance wrote some of it myself. How should this be solved? Does this kind of anti-pattern have a more "formal" name? Thanks for your help!