枚举中的业务逻辑? [英] Business logic in Enums?

查看:241
本文介绍了枚举中的业务逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将任何类型的业务逻辑放在枚举中是否被认为是好的做法?不是很强烈的逻辑,而是更多的便利效用方法。例如:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆