2011-06-27 54 views
0

好的我很确定这不是一个干净的实现。填充不同大小的数组

我有一个数组,我必须根据传入对象的属性填充属性。

我已经用一种相当脏的方式做了这件事,它回来咬我的屁股!

我们生病开始:

我有一个AccountsGRP[]。我这两种方法之间填充:

public static AccountGrp[] popAccArr(IncomingObject incObject) { 
    ArrayList<AccountGrp> accAL = new ArrayList<AccountGrp>(); 
    for (int i = 0; i < NoAccounts; i++) { 
     accAL.add(popAccAttr(i, incObject)); 
    } 
    AccountGrp[] AccountGrpArr = (AccountGrp[]) accAL 
      .toArray(new AccountGrp[accAL.size()]); 
    return AccountGrpArr; 
} 

private static AccountGrp popAccAttr(int i, IncomingObject incObject) { 
    AccountGrp acc = new AccountGrpImpl(); 
    switch (i) { 
    case 0: 
     acc.setAccount(incObject.getM_clientAcronym()); 
     acc.setAccountType(AccountType.CUST); 
     acc.setAcctIDSource(AcctIDSource.SLANG); 
     acc.setGrandParentAccount(incObject.getM_grandparentNum()); 
     return acc; 
    case 1: 
     acc.setAccount(incObject.getM_brokerAcronym()); 
     acc.setAccountType(AccountType.BKR); 
     acc.setAcctIDSource(AcctIDSource.SLANG); 
     // acc.setGrandParentAccount(incObject.getM_grandparentNum()); 
     return acc; 
    case 2: 
     acc.setAccount(incObject.getM_errorAccount()); 
     acc.setAccountType(AccountType.FIRM); 
     acc.setAcctIDSource(AcctIDSource.SLANG); 
     acc.setAccountSubType(AccountSubType.ERROR); 
     return acc; 
    default: 
     acc.setAccount(incObject.getM_clientAcronym()); 
     acc.setAccountType(AccountType.CUST); 
     acc.setAcctIDSource(AcctIDSource.SLANG); 
     acc.setGrandParentAccount(incObject.getM_grandparentNum()); 
     return acc; 
    } 
} 

这是坏的编码然而,我需要填充一些不同类型的账户,这样这个case语句是不灵活和肮脏。有没有这个我正在考虑的合适的实施。只是写的方法与许多参数需要在相关的值,然而,问题的出现喜欢:

acc.setAccountType(AccountType.BKR); 
     acc.setAcctIDSource(AcctIDSource.SLANG); 

哪些是返回枚举。也不是每个帐户迭代填充所有属性是否有一种方法来获得可选参数或者它只是一个重载的情况?

+1

啊,ol'For-case结构(http://thedailywtf.com/Articles/The_FOR-CASE_paradigm.aspx)!当弹出时总是一个经典的;) –

回答

1

我没有准确地再现您的具体条件的时间或精神刚毅,但这里有一个类似的代码片段应让你开始:

public static AccountGrp[] popAccArr(IncomingObject incObject) { 
    AccountGrp[] ret = new AccountGrp[NoAccounts]; 

    if(ret.length > 0) { 
     ret[0] = //first one 
    } 

    if(ret.length > 1) { 
     ret[1] = //second one 
    } 

    if(ret.length > 2) { 
     ret[2] = //third one 
    } 

    for(int i = 3; i < ret.length; i++) { 
     ret[i] = //nth one 
    } 

    return ret; 
} 
+0

+1“fortitude” –

0

这不是关于数组,而是如何将一个整数(i = [0-2])映射到一组包含枚举值的各种参数,每种参数可能有不同类型(子类型,祖父引用)。

重载popAccAttr不会帮助,因为调用者将不得不选择正确的重载。这只是将问题转移给调用者。你仍然需要映射i => params。

在我看来,清理它的最好方法是去除不透明的整数i。这个方法之外的“2”是什么意思?您可以使用一个枚举,它提供了所有可能的帐户类型的列表以及每个帐户的映射。它看起来像账户类型本身就足够了。因此,(还删除“0”之间的冗余和“默认”的情况下),

public static AccountGrp[] popAccArr(IncomingObject incObject) { 
    ArrayList<AccountGrp> accAL = new ArrayList<AccountGrp>(); 
    for (AccountType type : AccountType.values()) { // enumerate values 
     accAL.add(popAccAttr(type, incObject)); 
    } 
    AccountGrp[] AccountGrpArr = (AccountGrp[]) accAL 
      .toArray(new AccountGrp[accAL.size()]); 
    return AccountGrpArr; 
} 

private static AccountGrp popAccAttr(AccountType type, IncomingObject incObject) { 
    AccountGrp acc = new AccountGrpImpl(); 

    acc.setAccountType(type); // common for all 

    switch (type) { 
    case CUST: 
     acc.setAccount(incObject.getM_clientAcronym()); 
     acc.setAcctIDSource(AcctIDSource.SLANG); 
     acc.setGrandParentAccount(incObject.getM_grandparentNum()); 
     break; 
    case BKR: 
     acc.setAccount(incObject.getM_brokerAcronym()); 
     acc.setAcctIDSource(AcctIDSource.SLANG); 
     // acc.setGrandParentAccount(incObject.getM_grandparentNum()); 
     break; 
    case FIRM: 
     acc.setAccount(incObject.getM_errorAccount()); 
     acc.setAcctIDSource(AcctIDSource.SLANG); 
     acc.setAccountSubType(AccountSubType.ERROR); 
     break; 
    default: throw new IllegalArgumentException("unsupported account type: "+type); 
    } 

    return accc; // common for all 
} 
0

你OO设计很差:你不应该有一个AccountGrp[],它应该是一个类代替,很好地与命名的属性。您的代码会更易读和可维护。

如果你想坚持你目前的设计,你应该至少分割你的popAccAttr方法。我没有看到需要将3种完全不同的方法放入其中,并带有个案开关。

public static AccountGrp[] popAccArr(IncomingObject incObject) { 
    AccountGrp[] accountGrp = new AccountGrp[noAccounts]; 
    accountGrp[0] = popAccClient(incObject); 
    accountGrp[1] = popAccBroker(incObject); 
    accountGrp[2] = popAccError(incObject); 
    return AccountGrpArr; 
}