java - 我如何用枚举中每种环境类型的值列表来表示键?

标签 java oop design-patterns data-structures enums

我有两个环境 PRODSTAGING。在 prod 环境中,我们有三个数据中心 ABCDEFPQR,staging 有一个数据中心 CORP。每个数据中心都有几台机器,我为它们定义了常量,如下所示:

// NOTE: I can have more machines in each dc in future
public static final ImmutableList<String> ABC_SERVERS = ImmutableList.of("tcp://machineA:8081", "tcp://machineA:8082");
public static final ImmutableList<String> DEF_SERVERS = ImmutableList.of("tcp://machineB:8081", "tcp://machineB:8082");
public static final ImmutableList<String> PQR_SERVERS = ImmutableList.of("tcp://machineC:8081", "tcp://machineC:8082");

public static final ImmutableList<String> STAGING_SERVERS = ImmutableList.of("tcp://machineJ:8087","tcp://machineJ:8088");

现在我在同一个类中定义了另一个常量,它按 DC 分组为每种环境类型的机器列表。

public static final ImmutableMap<Datacenter, ImmutableList<String>> PROD_SERVERS_BY_DC =
  ImmutableMap.<Datacenter, ImmutableList<String>>builder()
      .put(Datacenter.ABC, ABC_SERVERS).put(Datacenter.DEF, DEF_SERVERS)
      .put(Datacenter.PQR, PQR_SERVERS).build();

public static final ImmutableMap<Datacenter, ImmutableList<String>> STAGING_SERVERS_BY_DC =
  ImmutableMap.<Datacenter, ImmutableList<String>>builder()
      .put(Datacenter.CORP, STAGING_SERVERS).build();

现在在其他一些类中,根据我在 (Utils.isProd()) 中的环境,我得到 PROD_SERVERS_BY_DCSTAGING_SERVERS_BY_DC.

Map<Datacenter, ImmutableList<String>> machinesByDC = Utils.isProd() ? Utils.PROD_SERVERS_BY_DC : Utils.STAGING_SERVERS_BY_DC;

现在我认为这可以用某种枚举以更好的方式表示,而不是像上面那样定义常量,但我无法弄清楚我该怎么做?我从这个开始,但对如何为每个 DC 使用单个键然后将多个值作为该 DC 的机器列表感到困惑,然后我还需要按环境对它们进行分组。

// got confuse on how can I make key (DC) and list of values (machines) for each environment type.
public enum DCToMachines {
  abc(tcp://machineA:8081", "tcp://machineA:8082"), 

  private final List<String> machines;
  private final Datacenter datacenter;
  ...


}

最佳答案

I started off with this but got confuse on how can I have single key for each DC and then multiple values as List of machines for that DC and then I need to group them by environment as well.

你忘记了一个重要的只是:枚举可以存储特定于每个枚举值的成员(方法和字段),但如果需要它也可以存储 static 成员。
您需要的是直接在枚举类中移动按环境分组的 map :

private static final ImmutableMap<Datacenter, ImmutableList<String>> PROD_SERVERS_BY_DC;
private static final ImmutableMap<Datacenter, ImmutableList<String>> STAGING_SERVERS_BY_DC;

您可以使用内部 map 根据用户请求返回预期的 map :

private static final Map<Env, ImmutableMap<Datacenter, ImmutableList<String>>> SERVERS_BY_ENV;

Env 是另一个枚举:

public static enum Env {
    PROD, STAGING
}

在我将要展示的代码中,我添加了 Datacenter 的封闭类型,但它在自己的文件中可能会更好,因为环境可能不是数据中心专门使用的概念。

最后,您用来按环境检索服务器的方式将相关职责分为 2 个类(Datacenter 枚举和当前类):

Map<Datacenter, ImmutableList<String>> machinesByDC = Utils.isProd() ? Utils.PROD_SERVERS_BY_DC : Utils.STAGING_SERVERS_BY_DC;

在枚举中引入一个方法来执行此操作会更好:

public static ImmutableMap<Datacenter, ImmutableList<String>> getServers(Env env){...}

这会增加枚举类的凝聚力。

这是更新的枚举:

import java.util.HashMap;
import java.util.Map;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;

public enum Datacenter {

   ABC(Env.PROD, "tcp://machineA:8081", "tcp://machineA:8082"), 
   DEF(Env.PROD, "tcp://machineB:8081", "tcp://machineB:8082"), 
   PQR(Env.PROD, "tcp://machineA:8081", "tcp://machineA:8082"), 
   CORP(Env.STAGING, "tcp://machineC:8081", "tcp://machineC:8082");

    public static enum Env {
        PROD, STAGING
    }

    private Env env;
    private String[] url;

    private static final ImmutableMap<Datacenter, ImmutableList<String>> PROD_SERVERS_BY_DC;
    private static final ImmutableMap<Datacenter, ImmutableList<String>> STAGING_SERVERS_BY_DC;
    private static final Map<Env, ImmutableMap<Datacenter, ImmutableList<String>>> SERVERS_BY_ENV;

    static {

        Builder<Datacenter, ImmutableList<String>> prodDataCenterBuilder = ImmutableMap.<Datacenter, ImmutableList<String>>builder();
        Builder<Datacenter, ImmutableList<String>> stagingDataCenterBuilder = ImmutableMap.<Datacenter, ImmutableList<String>>builder();

        for (Datacenter datacenter : Datacenter.values()) {
            if (datacenter.env == Env.PROD) {
                prodDataCenterBuilder.put(datacenter, ImmutableList.of(datacenter.url));
            } else if (datacenter.env == Env.STAGING) {
                stagingDataCenterBuilder.put(datacenter, ImmutableList.of(datacenter.url));
            }

            else {
                throw new IllegalArgumentException("not exepected env " + datacenter.env);
            }
        }

        PROD_SERVERS_BY_DC = prodDataCenterBuilder.build();
        STAGING_SERVERS_BY_DC = stagingDataCenterBuilder.build();

        SERVERS_BY_ENV = new HashMap<>();
        SERVERS_BY_ENV.put(Env.PROD, PROD_SERVERS_BY_DC);
        SERVERS_BY_ENV.put(Env.STAGING, STAGING_SERVERS_BY_DC);

    }

    Datacenter(Env env, String... url) {
        this.env = env;
        this.url = url;
    }    

    public static ImmutableMap<Datacenter, ImmutableList<String>> getServers(Env env) {
        ImmutableMap<Datacenter, ImmutableList<String>> map = SERVERS_BY_ENV.get(env);
        if (map == null) {
            throw new IllegalArgumentException("not exepected env " + env);
        }
        return map;
    }

}

这里是如何使用它:

public static void main(String[] args) {        
    ImmutableMap<Datacenter, ImmutableList<String>> servers = Datacenter.getServers(Env.PROD);
    servers.forEach((k, v) -> System.out.println("prod datacenter=" + k + ", urls=" + v));

    servers = Datacenter.getServers(Env.STAGING);
    servers.forEach((k, v) -> System.out.println("staging datacenter=" + k + ", urls=" + v));
}

输出:

prod datacenter=ABC, urls=[tcp://machineA:8081, tcp://machineA:8082]

prod datacenter=DEF, urls=[tcp://machineB:8081, tcp://machineB:8082]

prod datacenter=PQR, urls=[tcp://machineA:8081, tcp://machineA:8082]

staging datacenter=CORP, urls=[tcp://machineC:8081, tcp://machineC:8082]

关于java - 我如何用枚举中每种环境类型的值列表来表示键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47956136/

相关文章:

java - 存储 String 对象的最压缩方式

java - 如何在不使用instanceof的情况下处理多态事件

c# - Activator.CreateInstance 与工厂模式

python - 一个 Python 谜题

java - 如何使这部分代码可扩展,

Java Swing - 如何从 PlainDocument 将光标定位在 JTextField 内

java - 了解 'finally' block

java - Jogl着色器编程

python - 父访问在 Python 中子定义的类变量?

java - 工厂由a配置。对象的类 - 如何做得好?