I have a fairly lengthy if statement. The if statement examines a string “type” to determine what type of object should be instantiated. Here’s a sample…
if ( [type rangeOfString:@"coin-large"].location != NSNotFound )
{
... create large coin ...
mgr = gameLayer.coinLargeMgr;
}
else if ( [type rangeOfString:@"coin-small"].location != NSNotFound )
{
mgr = gameLayer.coinLargeMgr;
}
... more else statements ...
myObject = [mgr getNewObject];
The “else-if” statements continue for other object types which stand at about 20 right now and that number is likely to increase. This works quite well but in terms of maintenance and efficiency I think it could be improved. My leading candidate right now is to create an NSDictionary keyed on the object type string (coin-small, coin-large, etc.) and with the value of the manager object that should be tied to that type. The idea being that this would be a quick look for the type of object I need to create. Not sure this is the best approach, continuing to look at other options but am curious what folks here might have done for a similar problem. Any help/feedback is greatly appreciated.
The dictionary approach would be easily doable. Assuming the various managers have been boiled down to specific instances when you create the dictionary, it’d be just the same as almost any object-oriented language:
If all the managers do is vend appropriate objects, the more object-oriented approach might be:
That could save you having to write any managers if your program structure otherwise fits.