11

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;
 }

}
sma
  • 9,449
  • 8
  • 51
  • 80

6 Answers6

20

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?

abyx
  • 69,862
  • 18
  • 95
  • 117
10

Yes, i think this is a good idea. However, i think it can be implemented much more cleanly using instance methods:

public enum OrderStatus {

 OPEN, OPEN_WITH_RESTRICTIONS, OPEN_TEMPORARY, 
 CLOSED {
   @Override isOpen() { return false; }
 };

 public boolean isOpen()
 { 
   return true;
 }
}
james
  • 116
  • 2
6

I often use Enums for singleton instances. Thus they contain almost only business logic. Being classes that implcitly extend Enum they can even implement interfaces.

I'd only consider using Enums if it fits to the enumerated values, i.e. the buisness logic is tightly coupled with the instances.

b_erb
  • 20,932
  • 8
  • 55
  • 64
  • 3
    +1 for writing singletons using enums, more people should use that method. – Mateusz Dymczyk Jul 30 '10 at 17:25
  • 2
    @Tom Hawtin there's nothing wrong with singletons in general. They can and are used poorly, but that doesn't mean it's a bad design pattern. – Brian S Jul 30 '10 at 18:10
  • 3
    Singletons can be abused like anything else, but are definitely useful when used correctly. Even Josh Bloch suggests using enums for that purpose, b/c then you don't have to worry about serialization and other commonly missed details with hand rolled singleton classes – Java Drinker Jul 30 '10 at 18:14
3

Since the logic in your example is so closely tied to the (names of the) enumerated values, I can't think of a better place to put it.

tpdi
  • 34,554
  • 11
  • 80
  • 120
1

Enums primary job is the enforce a specific set of values with programmer friendly names. If you business logic can be expressed as a static set of values then Enums are a good way to go. Don't forget that in Java you can create Enum classes which hold more than one value, useful if you have a bunch of related values.

Michael Shopsin
  • 2,055
  • 2
  • 24
  • 43
0

For simple buisness logic as above it is ok, but i ran into a problem when it comes to the point of extending enums which is not possible. For example we stored additional data within the enums and later wanted to get a reverse mapping of the values for many different enums which would be easy with standard classes but is impossible with enums (without code duplication).
Standard classes can do the same (except of using in switch statements) and are more flexible and reliable. If the enumeration may get more complicated logic better use classes otherwise using enum with simple logic is quite fine in my opinion

Hatzen
  • 408
  • 2
  • 9
  • 16