DRYing 字符串分布在多个 类

DRYing String spread across several classes

我有一个 Spring 启动应用程序,它可以调用第三方应用程序的多个端点。 “start_date”在大多数调用中作为请求参数传递。今天,该字符串内联在进行调用的 classes 中(每个第三方端点都有一个 class)。我想擦干琴弦。

如果调用是从单个 class 发出的,我可以在那个 class 中有一个 private static final START_DATE = "start_date"。但是调用是多个 classes。有没有比 class 专用于保存值更好的解决方案?

注意:除了“start_date”之外,还有3或4个其他请求参数也是共享的。

您可以简单地使用 class 来保存这些值,而不是使用 private,而是使用 public。例如:

public class ApiConstants {
  public static final String START_DATE = "start_date";
}

然后您可以使用参数:

ApiConstants.START_DATE