第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

減少導(dǎo)致重復(fù)代碼的參數(shù)列表

減少導(dǎo)致重復(fù)代碼的參數(shù)列表

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

2 回答

?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊

BillBundle因?yàn)閰?shù)對(duì)象是一種可伸縮的方法,用于減少方法的參數(shù)列表。正如參數(shù)對(duì)象設(shè)計(jì)模式的任何實(shí)現(xiàn)一樣,也意味著在處理參數(shù)的代碼中移動(dòng)。避免代碼重復(fù)以獲取賬單并將其傳遞給方法的選項(xiàng)可能是訪問者設(shè)計(jì)模式(以獲取賬單)和模板方法設(shè)計(jì)模式(用于處理賬單)之間的混合depositCash

假設(shè)用于計(jì)算賬單的信息來自ATM,賬單發(fā)射器可以計(jì)算賬單并接受賬單處理器作為訪客,該處理器獲取賬單并對(duì)其進(jìn)行處理

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

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-09-14
?
狐的傳說

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

你的想法在我看來很好。你說你必須反復(fù)編寫這樣的代碼:BillBundle

Billbundle billBundle = new BillBundle();
billBundle.setFives(fives);
billBundle.setTens(tens);
billBundle.setTwenties(twenties);
billBundle.setFifties(fifties);
depositCash(billBundle);

你真的或者你只是在想你可能會(huì)嗎?

像這樣的事情只有在計(jì)算賬單時(shí)才應(yīng)該真正進(jìn)行,如果這種情況發(fā)生在不止幾個(gè)地方,我會(huì)感到驚訝。BillBundle

實(shí)際上不會(huì)更改或確定每個(gè)賬單數(shù)量的代碼應(yīng)該只是通過賬單捆綁包。記錄每種類型存入多少?gòu)堎~單的方法?通過它,你從計(jì)數(shù)得到。將總金額相加的方法?通過它,你從計(jì)數(shù)得到。等等等等。BillBundleBillBundle

這比到處傳遞4個(gè)參數(shù)要好得多,原因有兩個(gè):

  1. 搞砸事情的機(jī)會(huì)要少得多 - 每個(gè)采用4個(gè)賬單參數(shù)并將其傳遞到其他地方的函數(shù)都有機(jī)會(huì)以錯(cuò)誤的順序或錯(cuò)誤的參數(shù)位置傳遞它們。這尤其成問題,因?yàn)閷?shí)際的計(jì)數(shù)都是相同的類型(即 ),并且許多參數(shù)將該類型用于完全不同的事情。int

  2. 實(shí)際上,更少的代碼取決于您支持的賬單類型。假設(shè)你的國(guó)家換成5美元的硬幣,或者你只是不想再把它們放在機(jī)器里了......需要更改多少代碼才能擺脫五?您需要更改計(jì)算賬單的代碼以及實(shí)際關(guān)心每個(gè)賬單金額的所有其他內(nèi)容,但是您不必更改任何只是傳遞這些計(jì)數(shù)的代碼。他們可以傳遞原始文件,而不必?fù)?dān)心它。BillBundle

我建議的一個(gè)改變是讓你的比爾邦德爾不可變。然后,您不必?fù)?dān)心任何人在您傳遞它時(shí)更改它。

像這樣:

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;

    }

}

我會(huì)警告你,大多數(shù)Java程序員更喜歡getter方法而不是公共的最終字段,但是沒有充分的理由。


查看完整回答
反對(duì) 回復(fù) 2022-09-14
  • 2 回答
  • 0 關(guān)注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)