2011-06-27 56 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; 
}