用Objective-C中的替代构造替换大规模if语句的方法 [英] Ways to replace massive if statement with alternative construct in Objective-C

查看:104
本文介绍了用Objective-C中的替代构造替换大规模if语句的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当长的if语句. if语句检查字符串"type"以确定应实例化哪种类型的对象.这是一个示例...

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];

对于其他对象类型,"else-if"语句仍在继续,这些对象类型现在大约为20,并且该数量可能会增加.这工作得很好,但是在维护和效率方面,我认为可以改进.我现在的主要候选人是创建一个NSDictionary键,该键键入对象类型字符串(硬币较小,硬币较大等),并具有应该与该类型相关联的管理器对象的值.想法是,这将是我需要创建的对象类型的快速查看.不确定这是否是最好的方法,继续寻找其他选择,但对这里的人们可能为解决类似问题所做的事情感到好奇.任何帮助/反馈都将不胜感激.

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:

NSDictionary *stringsToManagers = 
    [NSDictionary dictionaryWithObjectsAndKeys:
         @"coin-large", gameLayer.coinLargeMgr,
         @"coin-small", gameLayer.coinSmallMgr,
         nil];

// this is assuming that type may contain multiple types; otherwise
// just use [stringsToManagers objectForKey:string]
for(NSString *string in [stringsToManagers allKeys])
{
    if([type rangeOfString:string].location != NSNotFound)
    {
         [[stringsToManagers objectForKey:string] addNewObject];
         // or get it and store it wherever it should go
    }
}

如果所有经理都在出售合适的对象,那么面向对象的方法可能是:

If all the managers do is vend appropriate objects, the more object-oriented approach might be:

NSDictionary *stringsToClasses = 
    [NSDictionary dictionaryWithObjectsAndKeys:
         @"coin-large", [LargeCoin class],
         @"coin-small", [SmallCoin class],
         nil];

// this is assuming that type may contain multiple types; otherwise
// just use [stringsToManagers objectForKey:string]
for(NSString *string in [stringsToManagers allKeys])
{
    if([type rangeOfString:string].location != NSNotFound)
    {
         id class = [stringsToManagers objectForKey:string];

         id newObject = [[class alloc] init];
         // this is exactly the same as if, for example, you'd
         // called [[LargeCoin alloc] init] after detecting coin-large
         // within the input string; you should obviously do something
         // with newObject now
    }
}

如果程序结构适合的话,这可以省去编写任何管理器的麻烦.

That could save you having to write any managers if your program structure otherwise fits.

这篇关于用Objective-C中的替代构造替换大规模if语句的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆