【发布时间】:2018-06-21 14:52:53
【问题描述】:
创建方法的更好方法
1)在DataUtils.java类中创建静态方法
或
2)在 pojo Claimheader.java 类中创建方法。
我的问题是,如果我们在 POJO 类中创建一个方法,它会影响任何性能。
在我们的项目中,我们在 POJO 类中添加了很多方法,只有 get 方法。那么在 POJO 类中创建 get 方法好吗? 或
在 Utility 类中创建方法。
我的老板说要在 POJO 类中创建 get 方法,而不是在任何实用程序类中创建。
请建议我哪种更好的方法来创建它。我认为如果我在 DataUtils.java 中创建方法而不是在 Pojo 类中创建方法是正确的。
请看下面的代码:
public class DataUtils {
public static String getCurrentResp(Integer currentResp) {
String val;
switch (currentResp) {
case 1:
val = "Primary";
break;
case 2:
val = "Secondary";
break;
case 3:
val = "Tertiary";
break;
default:
val = currentResp + "th Insurance";
break;
}
return val;
}
}
VS
@Entity
@Table(name = "claimheader")
@NamedQueries({
@NamedQuery(name = "ClaimHeader.getClaimByClaimNo", query = "SELECT clmhead FROM ClaimHeader as clmhead WHERE clmhead.claimNo = :arg1 "),
@NamedQuery(name = "claimHeader.GetUnPaidClaimByPatient", query = "SELECT clmhead FROM ClaimHeader as clmhead WHERE clmhead.insBalance > 0 and clmhead.patientCode = :arg1 "),
@NamedQuery(name = "claimHeader.GetUnPaidClaimByPatientCode", query = "SELECT clmhead FROM ClaimHeader as clmhead WHERE (clmhead.billedAmount = clmhead.balanceAmount) and clmhead.patientCode = :arg1 "),
@NamedQuery(name = "claimHeader.GetPaidClaimByPatientCode", query = "SELECT clmhead FROM ClaimHeader as clmhead WHERE (clmhead.balanceAmount = 0) and clmhead.patientCode = :arg1 "),
@NamedQuery(name = "claimHeader.GetUnPaidAndPaidClaimByPatientCode", query = "SELECT clmhead FROM ClaimHeader as clmhead WHERE clmhead.patientCode = :arg1 ") })
public class ClaimHeader extends BaseEntityByCode {
private static final long serialVersionUID = 1L;
@Id
@NotNull(message = "Claim No cannot be empty")
private Integer claimNo;
private Integer currentResp;
public Integer getCurrentResp() {
return currentResp;
}
public void setCurrentResp(Integer currentResp) {
this.currentResp = currentResp;
}
public String getCurrentRespDesc() {
String value = "";
if (currentResp == null)
return "";
switch (currentResp) {
case -1:
value = "Patient";
break;
case 1:
value = "Primary";
break;
case 2:
value = "Secondary";
break;
case 3:
value = "Tertiary";
break;
default:
value = currentResp.toString() + "th Insurance";
break;
}
return value;
}
}
【问题讨论】:
-
claimHeader.getCurrentRespDesc()比DataUtils.getCurrentResp(claimHeader.getCurrentResp())更简单 ... 由于描述取决于ClaimHeader的值,因此应该在其中。顺便说一句,你应该使用枚举而不是这个解决方案。 -
“在 POJO 类中创建 get 方法好吗?”...这是一个无意义的问题,恕我直言。 getter 应该返回实例的属性,因此它应该是 pojo 的一部分。如果它没有返回适合类实例的属性,则它不应该是 getter
-
@Sitansu 是否返回枚举或字符串并不真正相关。 Axel 是对的,您可能不会返回类实例的实例成员,但您确实返回了一个特定于类实例的值。将其保留在课程本身中。
-
@AxelH 在这种情况下不可能使用枚举,因为只有四个固定值,其余是具有 int 值和 "th Insurance的复合字符串>"
-
“我的问题是,如果我们在 POJO 类中创建一个方法,它会影响任何性能。” 永远不要为性能选择某种语法或构造除非您通过测量证明您实际上存在性能问题并且相关代码确实是瓶颈并且 b> 替代方案确实解决了问题。坚持使用最易读和/或最好地表达您的意图的版本。
标签: java performance methods pojo code-reuse