通过传递所需的参数来实例化类,具体取决于您所处的环境 [英] Instantiate class by passing required parameter depending on which environment you are in
问题描述
我有一个下面的枚举类,其中有几个字段stagePoolSize
,prodPoolSize
和enabled
.
I have a below enum class which has few fields in it stagePoolSize
, prodPoolSize
and enabled
.
public enum Type {
process_caty(5, 8, false), process_misc1(5, 8, false), link_entry(5, 12, true);
private final int stagePoolSize;
private final int prodPoolSize;
private final boolean enabled;
private Type(int stagePoolSize, int prodPoolSize, boolean enabled) {
this.stagePoolSize = stagePoolSize;
this.prodPoolSize = prodPoolSize;
this.enabled = enabled;
}
public int getStagePoolSize() {
return stagePoolSize;
}
public int getProdPoolSize() {
return prodPoolSize;
}
public boolean isEnabled() {
return enabled;
}
@Override
public String toString() {
return name() + "=" + stagePoolSize + "=" + prodPoolSize + "=" + enabled;
}
}
这就是我使用上面的枚举初始化我的Handler
类的方式.
And this is how I am using above enum to initialize my Handler
class.
private final List<Handler> handlers = new ArrayList<>();
@PostConstruct
public void postInit() {
String datacenter = Utils.getDatacenter();
for (Type consType : Type.values()) {
if (!consType.isEnabled()) {
continue;
}
int poolSize = Utils.isProd() ? consType.getProdPoolSize() : consType.getStagePoolSize();
handlers.add(new Handler(consType, poolSize));
}
}
如果启用了任何枚举,则根据我们所处的环境(无论是Prod还是Stage),通过对此调用Utils.isProd()
进行检查,我们得到它为poolSize
,然后使用该poolSize
进行初始化Handler
类.
If any of the enum is enabled, then depending on which environment (whether Prod or Stage) we are in by checking with this call Utils.isProd()
, we get it's poolSize
and then use that poolSize
to initialize Handler
class.
问题陈述:
现在,我需要在同一Type
类中添加几个枚举,但是我需要为它们做一些不同的事情.以下是我需要添加的枚举:
Now I need to add few more enums in the same Type
class but I need to do something different for them. Below are the enums I need to add:
abc_raw_pho
abc_raw_slq
abc_raw_lvk
abc_raw_pin_pho
abc_raw_pin_slq
abc_raw_pin_lvk
对于上述新枚举,这是我们需要做的:
Here is what we need to do for above new enums:
- 仅当环境为
Prod
时才应使用.它们将具有一些prodPool
大小值. - 如果我们处于
pho
数据中心,那么我们应该使用以pho
结尾的枚举.如果我们位于slq
数据中心,则应使用以slq
结尾的枚举. lvk也是如此.
- It should only be used if the environment is
Prod
. And they will have someprodPool
size value. - And also if we are in
pho
datanceter, then we should use enums that ends withpho
. And if we are inslq
datacenter then we should use enums that ends withslq
. Similarly for lvk.
通过调用此方法,我可以弄清楚我们位于哪个数据中心:
I can figure out which datacenter we are in by calling this method:
String datacenter = Utils.getDatacenter();
现在我应该如何设计我的Type
枚举类,以使我原来的枚举按原样工作,并且最终带有数据中心的新枚举也可以在上述条件下工作.因此,下面是总体要求:
Now how should I design my Type
enum class so that my original enum works as it is which is already there and also my new enum with datacenter in the end works with the above conditions. So below is the requirement overall:
-
process_caty
应该同时用于Stage
和Prod
,并且应该根据其所处的环境相应地使用poolSize. -
process_misc1
应该同时用于Stage
和Prod
,并且应该根据其所处的环境相应地使用poolSize. -
link_entry
应该同时用于Stage
和Prod
,并且应该根据其所处的环境相应地使用poolSize. -
abc_raw_pho
仅应用于Prod
,但要取决于我们所在的数据中心,并且它应仅将PoolSize用于Prod. -
abc_raw_slq
仅应用于Prod
,但要取决于我们所在的数据中心,并且应仅将PoolSize用于Prod. -
abc_raw_lvk
仅应用于Prod
,但要取决于我们所在的数据中心,并且它应仅将PoolSize用于Prod. -
abc_raw_pin_pho
应该仅用于Prod
,但要取决于我们所处的数据中心,并且应该仅将PoolSize用于Prod. -
abc_raw_pin_slq
应该仅用于Prod
,但取决于我们所处的数据中心,并且应该仅将PoolSize用于Prod. -
abc_raw_pin_lvk
应该仅用于Prod
,但要取决于我们所在的数据中心,并且应该仅将PoolSize用于Prod.
process_caty
should be use both forStage
andProd
and it should use poolSize accordingly depending on what environment it is in.process_misc1
should be use both forStage
andProd
and it should use poolSize accordingly depending on what environment it is in.link_entry
should be use both forStage
andProd
and it should use poolSize accordingly depending on what environment it is in.abc_raw_pho
should be use forProd
only but depending on which datacenter we are in and it should use poolSize for Prod only.abc_raw_slq
should be use forProd
only but depending on which datacenter we are in and it should use poolSize for Prod only.abc_raw_lvk
should be use forProd
only but depending on which datacenter we are in and it should use poolSize for Prod only.abc_raw_pin_pho
should be forProd
only but depending on which datacenter we are in and it should use poolSize for Prod only.abc_raw_pin_slq
should be forProd
only but depending on which datacenter we are in and it should use poolSize for Prod only.abc_raw_pin_lvk
should be forProd
only but depending on which datacenter we are in and it should use poolSize for Prod only.
推荐答案
我将重构您的枚举并将其分解为较小的类,为Environment
,DataCenter
和PoolSizes
引入模型:
I would refactor your enum and break it down into smaller classes, introducing models for Environment
, DataCenter
and PoolSizes
:
public enum Environment {PROD, STAGE}
public enum DataCenter {PHO, SLQ, LVK, NA /*Not applicable*/}
public class PoolSizes {
public static final int SIZE_UNDEFINED = -1;
private Map<Environment, Integer> environmentValues;
public PoolSizes() {
environmentValues = new HashMap<>();
}
public PoolSizes withStagePoolSize(int size) {
environmentValues.put(Environment.STAGE, size);
return this;
}
public PoolSizes withProductionPoolSize(int size) {
environmentValues.put(Environment.PROD, size);
return this;
}
public int getPoolSize(Environment environment) {
Integer size = environmentValues.get(environment);
return size != null ? size : SIZE_UNDEFINED;
}
}
然后,Type
枚举将使用它们简化查找池大小并确定是否启用该类型的逻辑:
The Type
enum would then use these to simplify the logic for poolsize lookup and determining whether the type is enabled or not:
public enum Type {
PROCESS_CATY(new PoolSizes().withStagePoolSize(5).withProductionPoolSize(8),
DataCenter.NA, false),
PROCESS_MISC1(new PoolSizes().withStagePoolSize(5).withProductionPoolSize(8),
DataCenter.NA, false),
LINK_ENTRY(new PoolSizes().withStagePoolSize(5).withProductionPoolSize(12),
DataCenter.NA, true),
ABC_RAW_PHO(new PoolSizes().withProductionPoolSize(123), DataCenter.PHO, true),
ABC_RAW_SLQ(new PoolSizes().withProductionPoolSize(123), DataCenter.SLQ, true),
ABC_RAW_LVK(new PoolSizes().withProductionPoolSize(123), DataCenter.LVK, true),
ABC_RAW_PIN_PHO(new PoolSizes().withProductionPoolSize(123), DataCenter.PHO, true),
ABC_RAW_PIN_SLQ(new PoolSizes().withProductionPoolSize(123), DataCenter.SLQ, true),
ABC_RAW_PIN_LVK(new PoolSizes().withProductionPoolSize(123), DataCenter.LVK, true);
private final PoolSizes poolSizes;
private final DataCenter dataCenter;
private final boolean enabled;
private Type(PoolSizes poolSizes, DataCenter dataCenter, boolean enabled) {
this.poolSizes = poolSizes;
this.dataCenter = dataCenter;
this.enabled = enabled;
}
public int getPoolSize(Environment environment) {
return poolSizes.getPoolSize(environment);
}
public DataCenter getDataCenter() {
return this.dataCenter;
}
public boolean isEnabled(Environment environment, DataCenter dataCenter) {
return enabled && poolSizes.getPoolSize(environment) != PoolSizes.SIZE_UNDEFINED
&& (getDataCenter() == DataCenter.NA || getDataCenter() == dataCenter);
}
}
最后,postInit()
方法将变成这样:
Finally, the postInit()
method would become something like this:
public void postInit() {
DataCenter dataCenter = Utils.getDataCenter();
Environment environment = Utils.getEnvironment();
for (Type consType : Type.values()) {
if (!consType.isEnabled(dataCenter, environment)) {
continue;
}
handlers.add(new Handler(consType, consType.getPoolSize(environment)));
}
}
这篇关于通过传递所需的参数来实例化类,具体取决于您所处的环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!