[英]populating an array of varying size
好的,我很確定這不是一個干凈的實現。
我有一個屬性數組,必須根據傳入對象的屬性進行填充。
我已經用一種相當骯臟的方式來做到這一點,但它又回來咬我了!
我們開始吧:
我有一個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);
哪些返回枚舉。 也不是每個帳戶迭代都會填充所有屬性,是否有一種獲取可選參數的方式,或者僅僅是重載的一種情況?
我沒有時間或精力去准確地再現您的確切條件,但是以下類似的代碼片段可以幫助您入門:
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;
}
這與數組無關,而是如何將整數(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
}
您的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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.