亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

減少導致重復代碼的參數列表

減少導致重復代碼的參數列表

元芳怎么了 2022-09-14 16:03:47
我正在處理ATM機項目,我經常需要將賬單金額作為參數傳遞給函數。有許多函數看起來類似于depositCash(int fives, int tens, int twenties, int fifties){...}我知道使用具有太多參數(例如4)的函數是不明智的做法。因此,我試圖將這些參數捆綁到一個新的類 billBundle 中,并將其作為參數傳遞。但后來我遇到了一個新問題,即反復編寫重復的代碼,如下所示:Billbundle billBundle = new BillBundle(); billBundle.setFives(fives); billBundle.setTens(tens); billBundle.setTwenties(twenties); billBundle.setFifties(fifties); depositCash(billBundle);如果我只是通過所有法案到法案捆綁,那么這將完全做我試圖避免的事情。我應該如何處理這個問題?謝謝。
查看完整描述

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

    }

}


查看完整回答
反對 回復 2022-09-14
?
狐的傳說

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個參數要好得多,原因有兩個:

  1. 搞砸事情的機會要少得多 - 每個采用4個賬單參數并將其傳遞到其他地方的函數都有機會以錯誤的順序或錯誤的參數位置傳遞它們。這尤其成問題,因為實際的計數都是相同的類型(即 ),并且許多參數將該類型用于完全不同的事情。int

  2. 實際上,更少的代碼取決于您支持的賬單類型。假設你的國家換成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方法而不是公共的最終字段,但是沒有充分的理由。


查看完整回答
反對 回復 2022-09-14
  • 2 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號