2 回答

TA貢獻1810條經驗 獲得超4個贊
在類 Foo 中,您可以將@Getter和@Setter注釋放在類上以避免重復(請注意,如果在此類上添加另一個不能公開的私有屬性,則應刪除@Getter注釋,以免公開私有屬性):
@Getter
@Setter
public class Foo
關于getWithBytesAndValues方法,我建議你把這種方法放在Foo類中,因為這種方法使get on Foo attrs并設置另一個Foo attrs(業務規則來自域Foo - 參見域驅動設計)。您還可以通過兩種方法分離內部邏輯:
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);
}
}
}
}

TA貢獻1818條經驗 獲得超11個贊
每個人都原諒我。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
);
添加回答
舉報