在iPhone开发的过程中经常会遇到根据不同的Table行或者标识符推入不同的Controller的需要,一个最直接的实现就是硬编码,通过if…else if…else或者switch…case…实现,代码如下:
重构前:
- (void)pushViewControllerWithIdentifier:(NSString *)identifier animated:(BOOL)animated {
if ([identifier isEqualToString:@"Article"]) {
viewController = [[WHArticleViewController alloc] initWithIdentifier:identifier];
} else if ([identifier isEqualToString:@"SurvivalKit"]) {
viewController = [[WHSurvivalKitViewController alloc] init];
} else if ([identifier isEqualToString:@"Search"]) {
viewController = [[WHSearchViewController alloc] initWithIdentifier:identifier];
} else if ([identifier isEqualToString:@"Featured"]) {
viewController = [[WHFeaturedViewController alloc] initWithIdentifier:identifier];
} else if ([identifier isEqualToString:@"Image"]) {
viewController = [[WHImageViewController alloc] initWithIdentifier:identifier];
} else if ([identifier isEqualToString:@"Settings"]) {
viewController = [[WHSettingsViewController alloc] init];
}
[self pushViewController:viewController animated:animated];
[viewController release];
}
其中的“坏味道”就是过多的条件分支判断以及在各个分支中雷同的代码。在需要扩展的时候就要被迫修改代码,从而破坏了开放封闭原则。总结下规律后觉得应该构建一个从identifier到对应的Controller名之间映射的表或者字典模型。实现的时候通过setupModel构建映射表,然后具体使用的时候利用表进行映射,重构代码如下:
重构一:
- (void) setupModel {
if(self.model == nil) {
self.model = [NSDictionary dictionaryWithObjectsAndKeys:@"WHArticleViewController", @"Article",
@"WHSurvivalKitViewController", @"SurvivalKit",
@"WHSurvivalKitViewController", @"Search",
@"WHFeaturedViewController", @"Featured",
@"WHImageViewController", @"Image",
@"WHSettingsViewController", @"Settings",
nil];
}
}
- (void)pushViewControllerWithIdentifier:(NSString *)identifier animated:(BOOL)animated {
NSString *identifierNamespace = [identifier identifierNamespace];
NSString *controllerClassName = [self.model objectForKey:identifierNamespace];
Class klass = NSClassFromString(controllerClassName);
UIViewController *viewController = [[klass alloc] initWithIdentifier:identifier];
[self pushViewController:viewController animated:animated];
[viewController release];
}
重构后,不仅避免了过多的条件分支而且清理了各个分支中的雷同代码。不足就是表模型数据还是夹杂在代码中,因此希望通过plist文件隐藏这些和代码无关的数据。重构代码如下:
重构二:
- (void) setupModel{
if(self.model == nil) {
NSString *plistFilePath = [[NSBundle mainBundle] pathForResource:@"Model" ofType:@"plist"];
self.model = [NSDictionary dictionaryWithContentsOfFile:plistFilePath];
}
}
- (void)pushViewControllerWithIdentifier:(NSString *)identifier animated:(BOOL)animated {
//实现同“重构一”
}
现在,一切看上去还挺美的。不过可能一段时间后会发现,原来indentifier和Controller名之间的映射是有规律的,所以可能根本不需要映射表。尝试一下吧!
重构三:
- (void)pushViewControllerWithIdentifier:(NSString *)identifier animated:(BOOL)animated {
NSString *identifierNamespace = [identifier identifierNamespace];
NSString *controllerClassName = [NSString stringWithFormat:@"WH%@ViewController", identifierNamespace];
Class klass = NSClassFromString(controllerClassName);
UIViewController *viewController = [[klass alloc] initWithIdentifier:identifier];
[self pushViewController:viewController animated:animated];
[viewController release];
}
经过几个阶段的重构,代码不仅“瘦身”了,而且逻辑更清晰了。通过这样一个从硬编码到模型到规律的过程,大家看到的应该不仅仅是不断改进的代码,而且还会感觉到重构的迭代性和无止境吧!任何的设计和实现都只能是在某种情境和阶段是合理的,而不存在一个永远完美的答案。