Java – should enumerated objects be stateless?
According to the design, the enumeration constant in Java is a singleton. For concurrent use, I usually create stateless enumeration instances and inject data as needed using method parameters
Example:
I am currently creating a rest service with operations (implemented as enumeration using a variant of the policy 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 operations are called and how often they succeed
I can save the state externally when using enumerated instances, but it seems that the state should be saved in operation because the operation should contain its own state
Now my general question is:
Is stateful enumeration of instances (other than concurrency problems) a bad design?
Solution
I think it violates the principle of minimum surprise
It is expected that the common use of enumeration was originally designed - as a constant or tag, rather than a generic class with state