我有办法将这3种方法合并为1吗?

时间:2012-07-21 20:28:04

标签: objective-c encapsulation

基本上,我有3个委托方法几乎完全相同的代码,除了每个方法中的1行和参数。我意识到我可以封装很多代码来在每种方法中生成3或4行代码,但我想知道是否有一种方法可以创建1种方法。

此外,tempData上调用的方法与下面的委托方法具有相同的方法名称,但它们实际上是不同的方法。

- (void)addElement:(NSString *)currentElement FromKeyboard:(NSString *)name {

    UIView *tempView; 
    NSMutableArray *tempViewList;
    EquationData *tempData;    


    if ([name isEqualToString:@"leftEqCellKeyboard"]) {
        tempData = leftData;
        tempViewList = leftEqViewList;
        tempView = equationCell.leftView;
    }

    if ([name isEqualToString:@"rightEqCellKeyboard"]) {
        tempData = rightData;
        tempViewList = rightEqViewList;
        tempView = equationCell.rightView;
    }

    [tempData addElement:currentElement]; // different

    if ([tempViewList count] != 0) 
        [self clearViewsStoredIn:tempViewList];
    [self setUpView:tempView fromArray:tempData.equation toArray:tempViewList];
}

- (void)changeState:(NSString *)stateName FromKeyboard:(NSString *)name {

   UIView *tempView; 
    NSMutableArray *tempViewList;
    EquationData *tempData;    

 if ([name isEqualToString:@"leftEqCellKeyboard"]) {
        tempData = leftData;
        tempViewList = leftEqViewList;
        tempView = equationCell.leftView;
    }

    if ([name isEqualToString:@"rightEqCellKeyboard"]) {
        tempData = rightData;
        tempViewList = rightEqViewList;
        tempView = equationCell.rightView;
    }

    [tempData changeState:stateName]; // different

    if ([tempViewList count] != 0) 
        [self clearViewsStoredIn:tempViewList];

       [self setUpView:tempView fromArray:tempData.equation toArray:tempViewList];

}

- (void)changeCharge:(NSString *)chargeIncrease FromKeyboard:(NSString *)name {

    UIView *tempView;
    NSMutableArray *tempViewList;
    EquationData *tempData;   

if ([name isEqualToString:@"leftEqCellKeyboard"]) {
        tempData = leftData;
        tempViewList = leftEqViewList;
        tempView = equationCell.leftView;
    }

    if ([name isEqualToString:@"rightEqCellKeyboard"]) {
        tempData = rightData;
        tempViewList = rightEqViewList;
        tempView = equationCell.rightView;
    }  
    [tempData changeCharge:chargeIncrease]; // different

    if ([tempViewList count] != 0) 
        [self clearViewsStoredIn:tempViewList];

    [self setUpView:tempView fromArray:tempData.equation toArray:tempViewList];


}

1 个答案:

答案 0 :(得分:0)

(也许你应该看看http://codereview.stackexchange.com

“不同”部分的格式始终为[tempData doSomethingOn:aString];,因此您可以使用选择器“doSomethingOn:”进行参数化:

-(void)doSomethingOnString:(NSString*)parameter 
              withSelector:(SEL)selector
              fromKeyboard:(NSString*)name 
{
    // The common parts before

    [tempData performSelector:selector withObject:parameter];

    // The common parts after
}

-(void)addElement:(NSString *)currentElement FromKeyboard:(NSString *)name {
    [self doSomethingOnString:currentElement
                 withSelector:@selector(addElement:)
                 fromKeyboard:name];
}

// etc.

另见: