public class GUIConfig {
private String name = "default";
// more private declarations
public GUIConfig() {
// constructor, just for setting defaults
}
// getters and setters
}
现在您可以简单地创建 GUI 类实例,如下所示:
GUIConfig guiConfig = new GUIConfig();
guiConfig.setName("foo");
// more setters
GUI myGUI = new GUI(guiConfig);
或仅使用默认值:
GUI myGUI = new GUI(new GUIConfig());
You can create a configuration class which holds default values for all parameters:
public class GUIConfig {
private String name = "default";
// more private declarations
public GUIConfig() {
// constructor, just for setting defaults
}
// getters and setters
}
Now you can simply create your GUI class instance like this:
GUIConfig guiConfig = new GUIConfig();
guiConfig.setName("foo");
// more setters
GUI myGUI = new GUI(guiConfig);
@AutoValue
public abstract class Card {
@Nullable
public abstract Integer localId();
public abstract Long utcStart();
public abstract Integer paramA();
public abstract Integer paramB();
@Nullable
public abstract Boolean paramC();
public static Builder builder() {
return new AutoValue_Card.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setLocalId(final Integer localId);
public abstract Builder setUtcStart(final Long utcStart);
public abstract Builder setParamA(final Integer paramA);
public abstract Builder setParamB(final Integer paramB);
public abstract Builder setParamC(final Boolean paramC);
public abstract Card build();
}
}
所有非必填字段都可以使用 @Nullable 进行注释。
要创建不可变的 Card 对象,您只需使用以下内容:
Card myCard = Card.builder()
.setLocalId(123456) // this line can be omitted
.setUtcStart(158632478000)
.setParamA(5)
.setParamB(58)
.setParamC(true) // this line can be omitted
.build();
@AutoValue
public abstract class Card {
@Nullable
public abstract Integer localId();
public abstract Long utcStart();
public abstract Integer paramA();
public abstract Integer paramB();
@Nullable
public abstract Boolean paramC();
public static Builder builder() {
return new AutoValue_Card.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setLocalId(final Integer localId);
public abstract Builder setUtcStart(final Long utcStart);
public abstract Builder setParamA(final Integer paramA);
public abstract Builder setParamB(final Integer paramB);
public abstract Builder setParamC(final Boolean paramC);
public abstract Card build();
}
}
All the no-mandatory fields can be annotated with @Nullable.
To create an immutable Card object you just use this:
Card myCard = Card.builder()
.setLocalId(123456) // this line can be omitted
.setUtcStart(158632478000)
.setParamA(5)
.setParamB(58)
.setParamC(true) // this line can be omitted
.build();
"AutoValue is a great tool for eliminating the drudgery of writing mundane value classes in Java. It encapsulates much of the advice in Effective Java Chapter 2, and frees you to concentrate on the more interesting aspects of your program. The resulting program is likely to be shorter, clearer, and freer of bugs. Two thumbs up."
发布评论
评论(4)
在这种情况下,通常会使用具有流畅语法的构建器对象。你改变
:
Often a builder object with fluent syntax is used in such a case. You change:
to
您可以创建一个配置类,其中包含所有参数的默认值:
现在您可以简单地创建 GUI 类实例,如下所示:
或仅使用默认值:
You can create a configuration class which holds default values for all parameters:
Now you can simply create your GUI class instance like this:
or for using only defaults:
使用 DTO(数据传输对象)来保存所有类。然后可以将其传递到单个参数中。
Use a DTO (Data Transfer Object) to hold all your classes. This can then be passed in a single parameter.
您应该考虑使用 Google 的 AutoValue 库:https://github。 com/google/auto/blob/master/value/userguide/index.md
所有非必填字段都可以使用
@Nullable
进行注释。要创建不可变的 Card 对象,您只需使用以下内容:
“AutoValue 是一个很棒的工具,可以消除用 Java 编写平凡值类的苦差事。它封装了《Effective Java》第 2 章中的大部分建议,使您能够专注于更有趣的事情。你的程序的各个方面可能会更短、更清晰、更没有错误。”
——Joshua Bloch,《Effective Java》作者
You should consider using Google's AutoValue library: https://github.com/google/auto/blob/master/value/userguide/index.md
All the no-mandatory fields can be annotated with
@Nullable
.To create an immutable Card object you just use this:
"AutoValue is a great tool for eliminating the drudgery of writing mundane value classes in Java. It encapsulates much of the advice in Effective Java Chapter 2, and frees you to concentrate on the more interesting aspects of your program. The resulting program is likely to be shorter, clearer, and freer of bugs. Two thumbs up."
-- Joshua Bloch, author, Effective Java