Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should enum objects be stateless?

Tags:

java

oop

enums

As by design an enum constant in java is a singleton, and for sake of concurrent usage I normally create stateless enum instances and use method parameters to inject the data as needed.

Example:

Currently I am creating a REST service which has Operations (implemented as an enum using a variant of the strategy pattern).

public enum Operation {

  DO_THIS() {
    public Result doSomething(Object theData) {
    }
  } ,
  // Other Operations go here
  ;

  public abstract Result doSomething(Object theData);

}

Now I want to collect data about how often an operation has been called and how often it succeeded and the like.

I could save the state externally when using the enum instance but it rather seems that the state should be saved in the Operation as the operation should contain it's own state.

Now my general question is:

Is a stateful enum instance (besides from concurrency issues) a bad design?

like image 856
Daniel Hiller Avatar asked Feb 12 '09 06:02

Daniel Hiller


4 Answers

I think it violates the Principle of Least Astonishment.

People expect the common usage of enums as they were originally designed - as constants or tokens, and not as general purpose classes with state.

like image 84
mparaz Avatar answered Oct 14 '22 07:10

mparaz


Yes. And by 'yes' I mean 'Always'.

If you want to collate stats on the number of operations called, implement some observability.

like image 28
cletus Avatar answered Oct 14 '22 09:10

cletus


Any form of mutable static is a sin. (Well, you might get away with non-leaky caches, some lazy initialisation and forms of logging.)

like image 38
Tom Hawtin - tackline Avatar answered Oct 14 '22 08:10

Tom Hawtin - tackline


A stateful enumeration is an oxymoron, even an anti-pattern!

http://en.wikipedia.org/wiki/Enumeration

An enumeration is a collection of items that is a complete, ordered listing of all of the items in that collection. The term is commonly used in mathematics and theoretical computer science to refer to a listing of all of the elements of a set. In statistics the term categorical variable is used rather than enumeration. The precise requirements for an enumeration (for example, whether the set must be finite, or whether the list is allowed to contain repetitions) depend on the branch of mathematics and the context in which one is working.

Enumerations have a finite number of values, which are supposed to be constant, which they are.

However, the fact that they are "first class" Java Objects totally goes against the grain of the intention or spirit of an enumeration.

If any kind of state is required, the enum (as mentioned earlier) should hold state in an Aspect or the offending enum, should at the very practical least, hold a reference to a delegate class holding state. Understanding "separation of concerns" will help.

like image 45
user924272 Avatar answered Oct 14 '22 09:10

user924272