可选班级成员
我从 XML 文件导入数据以在内部使用。现在有一个 uint 值,(根据 XSD)这不是必需的。现在的问题是:如何在我的类中映射此行为(尚不清楚该值是否存在,但我需要在运行时知道)
基本上我看到 3 个解决方案:
解决方案 1:使用我们知道的值将值标记为“未设置”是无效的:
public class Solution1 {
private int optionalVal;
public boolean isSetOptionalVal() {
return (optionalVal>=0);
}
public void setOptionalVal(int val) {
optionalVal = val;
}
public void unSetOptionalVal() {
optionalVal = -1;
}
public int optionalVal() {
if(isSetOptionalVal()) {
return optionalVal;
} else {
return -1;
}
}
}
解决方案 2:使用装箱类,如果值为“未设置”,则将其设置为 null
:
public class Solution2 {
private Integer optionalVal;
public boolean isSetOptionalVal() {
return (optionalVal!=null);
}
public void setOptionalVal(int val) {
optionalVal = val;
}
public void unSetOptionalVal() {
optionalVal = null;
}
public int optionalVal() {
if(isSetOptionalVal()) {
return optionalVal;
} else {
return -1;
}
}
}
解决方案 3:使用附加类将值描述为的变量'not-set':
public class Solution3 {
private int optionalVal;
private boolean optionalValSet;
public boolean isSetOptionalVal() {
return (optionalValSet);
}
public void setOptionalVal(int val) {
optionalVal = val;
optionalValSet = true;
}
public void unSetOptionalVal() {
optionalValSet = false;
}
public int optionalVal() {
if(isSetOptionalVal()) {
return optionalVal;
} else {
return -1;
}
}
}
这些是我解决问题的建议,但我并不喜欢其中任何一个。
解决方案1看起来很hacky,也许在某个地方我无法确定无效值。
解决方案2实际上是我正在使用的解决方案,但我只需要一些成员变量的附加信息,所以我要么使用一些变量作为装箱类型,一些作为原始变量(这似乎不一致),或者我总是使用盒装类型(我不太喜欢)。
解决方案3似乎是最干净的,但在这里我担心,在某些地方bool设置不正确,这将是一个很难发现的错误(我已经有很多代码,并发现了问题,一些最近没有在 XML 中设置元素)
那么...您希望用什么解决方案来解决“可选值”问题 - 是否有更好的解决方案? 这个问题一般是如何处理的?
I import data from a XML file to use it internally. Now there is an uint value, which is (according to the XSD) not required. Now here is the question: How do map this behaviour in my class (it is unclear, if the Value is present or not, but I need to know at runtime)
Basically I see 3 solutions:
Solution 1: Use values of which we know that they are invalid to flag the Value as 'not-set':
public class Solution1 {
private int optionalVal;
public boolean isSetOptionalVal() {
return (optionalVal>=0);
}
public void setOptionalVal(int val) {
optionalVal = val;
}
public void unSetOptionalVal() {
optionalVal = -1;
}
public int optionalVal() {
if(isSetOptionalVal()) {
return optionalVal;
} else {
return -1;
}
}
}
Solution 2: Use the boxed class and set it to null
if the value is 'not-set':
public class Solution2 {
private Integer optionalVal;
public boolean isSetOptionalVal() {
return (optionalVal!=null);
}
public void setOptionalVal(int val) {
optionalVal = val;
}
public void unSetOptionalVal() {
optionalVal = null;
}
public int optionalVal() {
if(isSetOptionalVal()) {
return optionalVal;
} else {
return -1;
}
}
}
Solution 3: Use an additional variable that describes the value as 'not-set':
public class Solution3 {
private int optionalVal;
private boolean optionalValSet;
public boolean isSetOptionalVal() {
return (optionalValSet);
}
public void setOptionalVal(int val) {
optionalVal = val;
optionalValSet = true;
}
public void unSetOptionalVal() {
optionalValSet = false;
}
public int optionalVal() {
if(isSetOptionalVal()) {
return optionalVal;
} else {
return -1;
}
}
}
These are my proposals to solve the issue, but I don't really like any of those.
Solution 1 seems very hacky, maybe there is somewhere a point where I can't determine the invalid value.
Solution 2 is actually the solution I am using, but I only need the additional information for some of the memeber variables, so I have either to use some variables as boxed types and some as primitive (which seems inconsistent) or I have always to use the boxed types (which I don't really like).
Solution 3 seems to be the cleanest, but here I am worried, that at some place the bool isn't set correctly, which would be a hard to find error (I already have a lot of code, and found the problem, that some elements are not set in the XML just recently)
So...what would you prefer as a solution to solve the "Optional Value"-problem - is there maybe an even better solution?
How is this problem generally handled?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我选择选项 2,使用 Integer 类,并将 int 和 Integer 之间的转换留给自动装箱。这种方法的优点是它将与可选值相关的所有内容保留在单个变量中。
第一个选项是一个神奇的值,如果未使用的值后来变成了已使用的值,那么维护起来就变成了一场噩梦。
第三个选项意味着必须跟踪 int 和布尔值,以跟踪是否使用它的问题。如果您打算这样做,请考虑将其本身作为一个类...但是您也可以使用 Integer。
I'd choose option 2, using the Integer class, and leave the conversion between int and Integer to autoboxing. The advantage of this approach is that it keeps everything concerned with your optional value in a single variable.
The first option is a magic value, and if the unused value becomes a used value later, it becomes a nightmare to maintain.
The third option means having to keep track of both the int and the boolean that keeps track of the question whether it is used. If you're going to do this, consider making it a class in and of itself... but then you might as well use Integer.
解决方案 2 是迄今为止最干净的。这正是
null
的用途,对某些值使用基本类型,对其他值使用包装类型,表明存在差异 - 没有任何不一致之处。Solution 2 is by far the cleanest. That's exactly what
null
is for, and using primitive types for some values and wrapper types for others communicates that there's a difference - theres nothing inconsistent about it.我也更喜欢解决方案 2,这也是我们通常使用的。
但请注意:您的 getter/setter 应该反映这一点,即它们应该看起来像这样(实际上可能是您的代码中的情况,但不是您的帖子中的情况):
因为您已经将
null
作为指示是否设置了可选值,我不会引入另一个值(在您的情况下为-1)。如果您需要一个很可能取决于该对象的用户的默认值。I'd also prefer solution 2, which is what we generally use as well.
Just a note though: your getters/setters should reflect that, i.e. they should look like this (which might actually be the case in your code but not in your post):
Since you already have
null
as the indicator whether the optional value is set or not, I'd not introduce another value (-1 in your case). If you need a default value that's most likely dependent on the user of that object.