当前位置:网站首页>(3/8) method parameters of improper use of enumeration (2)

(3/8) method parameters of improper use of enumeration (2)

2022-07-07 09:19:00 51CTO


 

 

Continue to talk about the use of enumeration . This article illustrates the crap of not enumerating method parameters .

【 On the first code 】

First look at the following methods :

      
      
private boolean updRpmRtulProcc(RpmRtulDO rpmRtulDO, String rfdSts)
throws YGException {
logger.infoFmt(" Failed to modify the refund order status [{}]", RefundStsEnum.getBySts(rfdSts).getDesc());
rpmRtulDO.setRfdSts(rfdSts);

... ...

return true;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

 

The call to this method in the program :

      
      
if (!updRpmRtulProcc(rpmRtul, RefundStsEnum.RFD_CLEARING_PROC.getSts())) {
... ...
}
  • 1.
  • 2.
  • 3.

 

The following is RefundStsEnum Enumeration class :

      
      
/**
* Refund status enumeration
*/
public enum RefundStsEnum {

RFD_PROCESS("0"," Refund processing "),
RFD_SUCC("3"," Refund successful "),
RFD_FAIL("4"," Refund failed "),
RFD_CANCEL("7"," Refund cancellation "),
RFD_CLEARING_PROC("8"," Refund settlement processing "),
RFD_ACCOUNTING_PROC("9"," Refund bookkeeping processing "),
REFUSE("R9", " Approval denied "),
RFD_APPROVAL("W3", " Refund approval in progress ");


private String sts;
private String desc;

RefundStsEnum(String sts, String desc) {
this.sts = sts;
this.desc = desc;
}

public String getSts() {
return sts;
}

public String getDesc() {
return desc;
}

public static RefundStsEnum getBySts(String sts) {
for (RefundStsEnum type : values()) {
if (type.getSts().equals(sts)) {
return type;
}
}
return null;
}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

 

【 Refactoring mode 】

In the above method String Variable rfdSts, The corresponding enumeration types are defined in the program RefundStsEnum.

This example is a typical example of improper enumeration . The crux is to set method parameters rfdSts Define a String, This leads to two things :

  1. Inside the method , Like the first line , When logging , First call enumeration RefundStsEnum Of getBySts(), take rfdSts Stored RefundStsEnum Of sts Convert to enumeration RefundStsEnum, then , Get it desc.
  2. The parameters passed by calling this method are RefundStsEnum.getSts()

The right way to do it is : Put the method parameters rfdSts Defined as an enumeration type RefundStsEnum, natural , There is no such unhappiness . It will be clear !

      
      
private boolean updRpmRtulProcc(RpmRtulDO rpmRtulDO, RefundStsEnum rfdSts)
throws YGException {
logger.infoFmt(" Failed to modify the refund order status [{}]", rfdSts.getDesc());
rpmRtulDO.setRfdSts(rfdSts.getSts());

... ...

return true;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

 

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070632300990.html