枚举中的业务逻辑? [英] Business logic in Enums?
本文介绍了枚举中的业务逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public enum OrderStatus {
OPEN,OPEN_WITH_RESTRICTIONS,OPEN_TEMPORARY,CLOSED;
public static boolean isOpenStatus(OrderStatus sts){
return sts == OPEN || sts == OPEN_WITH_RESTRICTIONS || sts == OPEN_TEMPORARY;
}
}
解决方案
IM HO IM。。。。。。。。。。。。。。。。。。。。。。没有理由枚举不是实际负责的实际课程。
如果这允许你编写更简单的代码,并且 SOLID 代码,为什么不?
Is it considered good practice to put any type of business logic in Enums? Not really intense logic, but more of like convenience utility methods. For example:
public enum OrderStatus {
OPEN, OPEN_WITH_RESTRICTIONS, OPEN_TEMPORARY, CLOSED;
public static boolean isOpenStatus(OrderStatus sts) {
return sts == OPEN || sts == OPEN_WITH_RESTRICTIONS || sts == OPEN_TEMPORARY;
}
}
解决方案
IMHO, this enables you to put relevant information right where it's likely to be used and searched for. There's no reason for enums not to be actual classes with actual responsibility.
If this allows you to write simpler code, and SOLID code, why not?
这篇关于枚举中的业务逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文