类构造函数中大量参数的优雅替代方案

发布于 2024-11-15 23:08:46 字数 1431 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

这样的小城市 2024-11-22 23:08:46

在这种情况下,通常会使用具有流畅语法的构建器对象。你改变

new XYZEvent(a, null, null, b, null, c, d, null, null)

new XYZEventBuilder().setA(a).setB(b).setC(c).setD(d).build()

Often a builder object with fluent syntax is used in such a case. You change:

new XYZEvent(a, null, null, b, null, c, d, null, null)

to

new XYZEventBuilder().setA(a).setB(b).setC(c).setD(d).build()
关于从前 2024-11-22 23:08:46

您可以创建一个配置类,其中包含所有参数的默认值:

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);

or for using only defaults:

GUI myGUI = new GUI(new GUIConfig());
玉环 2024-11-22 23:08:46

使用 DTO(数据传输对象)来保存所有类。然后可以将其传递到单个参数中。

DTO 没有任何行为
除了存储和检索
自己的数据

Use a DTO (Data Transfer Object) to hold all your classes. This can then be passed in a single parameter.

a DTO does not have any behavior
except for storage and retrieval of
its own data

等你爱我 2024-11-22 23:08:46

您应该考虑使用 Google 的 AutoValue 库:https://github。 com/google/auto/blob/master/value/userguide/index.md

@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 是一个很棒的工具,可以消除用 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

@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."

-- Joshua Bloch, author, Effective Java

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文