我正在构造具有如下数据结构的枚举值:
enum MetaType {
HUMAN(
new MetaTypeAttribute(AttributeType.BODY, 1, 6),
new MetaTypeAttribute(AttributeType.AGILITY, 1, 6),
new MetaTypeAttribute(AttributeType.REACTION, 1, 6)
),
ORK(
new MetaTypeAttribute(AttributeType.BODY, 4, 9),
new MetaTypeAttribute(AttributeType.AGILITY, 1, 6),
new MetaTypeAttribute(AttributeType.REACTION, 1, 6)
);
MetaType(MetaTypeAttribute body, MetaTypeAttribute agility, MetaTypeAttribute reaction) {
}
}
因此有一个 MetaType 枚举;每个 MetaType 指定一组固定的 MetaTypeAttributes,其中包含该属性的最小值和最大值。
我讨厌这种语法。它太长了,很难看,而且它并不能阻止我传递错误的 AttributeType(AttributeType 应与 MetaType 构造函数的参数顺序相匹配;AttributeType.BODY > body 等)。我想要的是一个更好的模式来确保使用这种精确的方案构造对象。
此代码是缩写的。实际上有 9 种 AttributeType。
有什么改进建议吗?
最佳答案
enum MetaType {
HUMAN(new MyRange(1,1), new MyRange(1, 10), new MyRange(1,10)
),
ORK(new MyRange(1,1), new MyRange(1, 10), new MyRange(1,10)
);
MetaType(MyRange body, MyRange agility, MyRange reaction) {
this.bodyAttrib = new MetaTaypAttibute(AttributeType.BODY, body);
this.agilityAttrib = new MetaTaypAttibute(AttributeType.AGILITY, agility);
this.reactionAttrib = new MetaTaypAttibute(AttributeType.REACTION, reactiony);}
}
关于java - 构建该对象的更好模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3641388/