gpt4 book ai didi

java - 当常量数量巨大时,如何正确定义常量?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:17:02 25 4
gpt4 key购买 nike

我正在编写一些需要定义大量常量的代码。它主要处理 Marketplace 常量(可能是美国、英国、印度、日本)和关联的 MarketplaceMerchantMapping,它基本上将 MerchantID 映射到 市场 ID

例如:

public enum Marketplace {

US("US"),
JP("JP"),
UK("UK"),
IN("IN"),
NZ("NZ"),
CA("CA"),
FR("FR"),
...
...

// This could go up to some 400 marketplaces

private final String stringValue;

public boolean isWest() {
return this == US || this == CA || this == UK;
}

public boolean isEast() {
return this == IN || this == NZ || this == JP;
}

}
public enum MarketplaceMerchantMapping {

USMAP(MarketplaceID.US, MerchantID.US, Marketplace.US),
JPMAP(MarketplaceID.JP, MerchantID.JP, Marketplace.JP),
UKMAP(MarketplaceID.UK, MerchantID.UK, Marketplace.UK),
NZMAP(MarketplaceID.NZ, MerchantID.NZ, Marketplace.NZ),
INMAP(MarketplaceID.IN, MerchantID.IN, Marketplace.IN),
CAMAP(MarketplaceID.CA, MerchantID.CA, Marketplace.CA),
FRMAP(MarketplaceID.FR, MerchantID.FR, Marketplace.FR),
...
...

// THis can go up to 400 Marketplaces * Number of merchantIds in each marketplace.


}

还有其他类似的常量,其定义类似于枚举或静态常量。

这确实不可扩展,因为每次我们添加对新市场和商户的支持时,我们都需要去更新一堆文件并测试它们,而这些配置更改本身会占用大量开发人员时间。

理想情况下,我想知道是否有某种方法可以在某些配置文件中定义这些常量并读取这些文件以创建常量。有没有办法通过读取和解析一些配置文件来创建这样的枚举常量?

所以如果我有一个包含以下条目的配置文件:

配置文件.cfg:

WestMarketplaces = ("US", "AG", "MX", "CA", ...) // Expand this later as required
EastMarketplaces = ("IN", "AU", "SG", "JP", ...) // Expand this later as required
EUMarketplaces = ("UK", "FR", "SP", ...) // Expand this later as required

WestMerchantIds = ("WA", "WB", "WC", "WD", ...) // Expand this later as required
WestMerchantIds = ("EA", "EB", "EC", "WD", ...) // Expand this later as required
EUMerchantIds = ("EUA", "EUB", "EUC", "WDD", ...) // Expand this later as required

marketplaceMerchantMapping = {
"US" = "WA";
"CA" = "WB";
"MX" = "WC";
"AU" = "EA";
"IN" = "EB";
"JP" = "EC";
"UK" = "EUA";
"FR" = "EUB";
...
} // Expand this later as required

然后,它能否从配置文件中读取这些常量并构建适当的枚举或静态常量?

这可能吗?

最佳答案

当无法使用数据库时,维护良好 XLS 表可能是下一个最佳选择。与经过良好测试的获取工作表数据的过程一起,从中生成代码,或用作某种属性配置输入。

正如 OP 自己认为的那样:当需要维护如此大量的“关键”属性时,Java 源代码并不理想,但更重要的是,需要定期更新。

当然,这里的关键方面是:维护良好。需要有一个每个人都必须遵循的明确定义的过程,并且整个“链”,从对 XLS 进行更改(最好以版本控制的方式)到生成构建时所需的工件应该简单且经过良好测试。

关于java - 当常量数量巨大时,如何正确定义常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56645234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com