2 回答
TA貢獻1820條經驗 獲得超10個贊
BillBundle因為參數對象是一種可伸縮的方法,用于減少方法的參數列表。正如參數對象設計模式的任何實現一樣,也意味著在處理參數的代碼中移動。避免代碼重復以獲取賬單并將其傳遞給方法的選項可能是訪問者設計模式(以獲取賬單)和模板方法設計模式(用于處理賬單)之間的混合depositCash
假設用于計算賬單的信息來自ATM,賬單發射器可以計算賬單并接受賬單處理器作為訪客,該處理器獲取賬單并對其進行處理
interface BillEmitter {
int getFives();
int getTens();
int getTwenties();
int getFifties();
default void accept(Visitor v) {
v.visit(this);
}
}
// add BillEmitter implementations as needed
public class SomeBillEmitter implements BillEmitter {
private Atm atm;
public SomeBillEmitter(Atm atm) {
this.atm = atm;
}
public int getFives() {
int theFivesBill = 0;
// compute the fives bill with the information from ATM
return theFivesBill;
}
public int getTens() {
int theTensBill = 0;
// compute the tens bill with the information from ATM
return theTensBill;
}
public int getTwenties() {
int theTwentiesBill = 0;
// compute the twenties bill with the information from ATM
return theTwentiesBill;
}
public int getFifties() {
int theFiftiesBill = 0;
// compute the fifties bill with the information from ATM
return theFiftiesBill;
}
}
參觀者
interface Visitor {
default void visit(BillEmitter billEmitter) {
// template method which gets the bills from the billEmitter
// and pass them to the bill processor
Billbundle billBundle = new BillBundle();
billBundle.setFives(billEmitter.getFives());
billBundle.setTens(billEmitter.getTens());
billBundle.setTwenties(billEmitter.getTwenties());
billBundle.setFifties(billEmitter.getFifties());
processBills(billBundle);
}
void processBills(BillBundle billBundle);
}
// add Visitor implementations as needed
public class DepositCashVisitor implements Visitor {
public void processBills(BillBundle billBundle) {
// deposit the cash
...
}
}
用法
public class Atm {
// add methods which returns information used to emit bills
}
public class Test {
public static void main(String[] args) {
Visitor depositCashVisitor = new DepositCashVisitor();
Atm atm = new Atm();
BillEmitter billEmitter = new SomeBillEmitter(atm);
billEmitter.accept(depositCashVisitor);
// add more bill emitters and visit them with depositCashVisitor
// or add more visitors and visit billEmitter with them
}
}
TA貢獻1804條經驗 獲得超3個贊
你的想法在我看來很好。你說你必須反復編寫這樣的代碼:BillBundle
Billbundle billBundle = new BillBundle(); billBundle.setFives(fives); billBundle.setTens(tens); billBundle.setTwenties(twenties); billBundle.setFifties(fifties); depositCash(billBundle);
但你真的或者你只是在想你可能會嗎?
像這樣的事情只有在計算賬單時才應該真正進行,如果這種情況發生在不止幾個地方,我會感到驚訝。BillBundle
實際上不會更改或確定每個賬單數量的代碼應該只是通過賬單捆綁包。記錄每種類型存入多少張賬單的方法?通過它,你從計數得到。將總金額相加的方法?通過它,你從計數得到。等等等等。BillBundleBillBundle
這比到處傳遞4個參數要好得多,原因有兩個:
搞砸事情的機會要少得多 - 每個采用4個賬單參數并將其傳遞到其他地方的函數都有機會以錯誤的順序或錯誤的參數位置傳遞它們。這尤其成問題,因為實際的計數都是相同的類型(即 ),并且許多參數將該類型用于完全不同的事情。
int實際上,更少的代碼取決于您支持的賬單類型。假設你的國家換成5美元的硬幣,或者你只是不想再把它們放在機器里了......需要更改多少代碼才能擺脫五?您需要更改計算賬單的代碼以及實際關心每個賬單金額的所有其他內容,但是您不必更改任何只是傳遞這些計數的代碼。他們可以傳遞原始文件,而不必擔心它。
BillBundle
我建議的一個改變是讓你的比爾邦德爾不可變。然后,您不必擔心任何人在您傳遞它時更改它。
像這樣:
class BillBundle
{
public final int fives;
public final int tens;
public final int twenties;
public final int fifties;
public BillBundle(int fives, int tens, int twenties, int fifties)
{
this.fives = fives;
this.tens = tens;
this.twenties = twenties;
this.fifties = fifties;
}
}
我會警告你,大多數Java程序員更喜歡getter方法而不是公共的最終字段,但是沒有充分的理由。
添加回答
舉報
