我可以重构这个不使用方法调用吗?

时间:2012-08-20 18:43:48

标签: objective-c cgrect

假设我有这些方法:

+(CCSprite *)a {
    return [self shapeFromRect:CGRectMake(0, 224, 224, 288) rotate:0];
}

+(CCSprite *)b {
    return [self shapeFromRect:CGRectMake(0, 224, 224, 288) rotate:0];
}

+(CCSprite *)c {
    return [self shapeFromRect:CGRectMake(0, 100, 50, 50) rotate:0];
}

+(CCSprite *)d {
    return [self shapeFromRect:CGRectMake(0, 100, 50, 50) rotate:90];
}

有冗余,所以我可以重构它:

+(CCSprite *)a {
    return [self shapeFromRect:[self abRect] rotate:0];
}

+(CCSprite *)b {
    return [self shapeFromRect:[self abRect] rotate:0];
}

+(CCSprite *)c {
    return [self shapeFromRect:[self cdRect] rotate:0];
}

+(CCSprite *)d {
    return [self shapeFromRect:[self cdRect] rotate:90];
}

+(CGRect)abRect {
    return CGRectMake(0, 224, 224, 288);
}

+(CGRect)cdRect {
    return CGRectMake(0, 100, 50, 50);
}

但我想知道,有更有效的方法吗?

有没有办法可以将这些CGRect存储在typedef枚举或其他形式的常量中,这样我就不必执行方法调用了?

1 个答案:

答案 0 :(得分:2)

+ (CCSprite *)sprite:(NSInteger)type
{
    float r = (type == 3)? 90 : 0;
    if (type < 2) {
        return [self shapeFromRect:CGRectMake(0, 224, 224, 288)] rotate:r];
    } else {
        return [self shapeFromRect:CGRectMake(0, 100, 50, 50)] rotate:r];
    }
}

也许类似的东西?