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

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

如何避免java類的這個(gè)幫助器方法中的代碼重復(fù)?

如何避免java類的這個(gè)幫助器方法中的代碼重復(fù)?

慕容3067478 2022-08-17 16:55:19
我有一個(gè)這樣的java類(通過龍目島的getters/setters):public class Foo{  @Getter  @Setter  private byte[] byte1;  @Getter  @Setter  private String byte1String;  @Getter  @Setter  private String byte1Value;  @Getter  @Setter  private byte[] byte2;  @Getter  @Setter  private String byte2String;  @Getter  @Setter  private String byte2Value;}以及以下用于填充對象值的幫助器方法:Fooprivate static final String DEFAULT_BYTE_VALUE = "someDefaultValue";private Foo getWithBytesAndValues(Foo foo){    if (foo.getByte1String() != null)    {        foo.setByte1(new Base64(foo.getByte1String()).decode());        if (foo.getByte1Value() == null)        {            foo.setByte1Value(DEFAULT_BYTE_VALUE);        }    }    if (foo.getByte2String() != null)    {        foo.setByte2(new Base64(foo.getByte2String()).decode());        if (foo.getByte2Value() == null)        {            foo.setByte2Value(DEFAULT_BYTE_VALUE);        }    }    return foo;}我的助手方法看起來很亂。而且似乎我重復(fù)了兩次相同的代碼。有沒有辦法簡化方法?
查看完整描述

2 回答

?
蝴蝶不菲

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

在類 Foo 中,您可以將@Getter和@Setter注釋放在類上以避免重復(fù)(請注意,如果在此類上添加另一個(gè)不能公開的私有屬性,則應(yīng)刪除@Getter注釋,以免公開私有屬性):


@Getter

@Setter

public class Foo

關(guān)于getWithBytesAndValues方法,我建議你把這種方法放在Foo類中,因?yàn)檫@種方法使get on Foo attrs并設(shè)置另一個(gè)Foo attrs(業(yè)務(wù)規(guī)則來自域Foo - 參見域驅(qū)動(dòng)設(shè)計(jì))。您還可以通過兩種方法分離內(nèi)部邏輯:


public class Foo {


...


    public void setBytes()

        setByte1();

        setByte2();

    }


    private void setByte2() {

        if (getByte2String() != null) {

            setByte2(new Base64(getByte2String()).decode());

            if (getByte2Value() == null) {

                setByte2Value(DEFAULT_BYTE_VALUE);

            }

        }

    }


    private void setByte1() {

        if (getByte1String() != null) {

            setByte1(new Base64(getByte1String()).decode());

            if (getByte1Value() == null) {

                setByte1Value(DEFAULT_BYTE_VALUE);

            }

        }

    }

}


查看完整回答
反對 回復(fù) 2022-08-17
?
慕尼黑8549860

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

每個(gè)人都原諒我。JB Nizet的建議要好得多,但我想看看我能做些什么。


public static void touch(

        final Consumer<byte[]> setByte,

        final Consumer<? super String> setByteValue,

        final Supplier<String> byteString,

        final Supplier<String> byteValue) {

    if (byteString != null) {

        setByte.accept(Base64.getDecoder().decode(byteString.get()));

    }


    if (byteValue.get() == null) {

        setByteValue.accept(DEFAULT_BYTE_VALUE);

    }

}

touch(

        foo::setByte1,

        foo::setByte1Value,

        foo::getByte1String,

        foo::getByte1Value

);


touch(

        foo::setByte2,

        foo::setByte2Value,

        foo::getByte2String,

        foo::getByte2Value

);


查看完整回答
反對 回復(fù) 2022-08-17
  • 2 回答
  • 0 關(guān)注
  • 104 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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