using setter to inject service into Enum...Bad Practice?
enums, java, spring
Solution
Enums are best used for things that MUST be differentiated in code--business logic. If you are using them for data (as your example), it doesn't make any sense to me.
Also, by data vs code I'm not talking about simply iterating over them, you actually have to have significantly different code USING different enums, otherwise they are just a (bad) data initialization device.
A better initialization of that type of data might be:
String[] init=new String[] {"WALK", "I am walking", "SKIP", "I am skipping", ...}
Map lookup=new HashMap();
for(int i=0;i+=2;i<init.length)
{
lookup.put(init[i],init[i+1])
}
No redundancy, much simpler, and when that list becomes more complicated it's trivial to take it outside of the code to a text, properties, xml or whatever flavor of data you prefer.
You can even associate code with these if that is what you are after by wrapping "Lookup" and this entire initialization into an object (A good idea) I'd make something that looked like this:
public class Motivate()
{
private static Map<String, Motivate> motivations;
private String action;
private String description;
private Motivate(String action, String description)
{
this.action=action;
this.description=description;
}
public void init()
{
if(motivations == null)
{
build motivations using all the stuff in the first example
}
}
}
If you want different code attached (assuming your examples were just trivial and each "Mode" needed different code), add a member that holds an interface like "Runnable" and pass that into the constructor when you build them.
Then your code should never refer to "RUN" or "WALK", it is just data that is bound, for instance, to a users keystroke or some other data.
Problem
Is this considered bad practice? Essentially, based on the enum I want to call a specific method in an interface. Each enum will have its own interface implementation (WalkImpl,RunImpl,JogIMpl, etc....) all based off of the ActivityService interface. I just wanted to know is this the right way to "inject" a service into an Enum. I am doing it this way since I can't autowire the service. Thanks ``` @Component public class HelloWorldImpl implements HelloWorld { private enum MyEnum{ WALK { @Override public void execute() { System.out.println("I am walking"); activityService.doSomethingWithMe(this.name()); } }, RUN{ @Override public void execute() { System.out.println("I am running"); } },JOG{ @Override public void execute() { System.out.println("I am jogging!"); } }, SKIP{ @Override public void execute() { System.out.println("I am skipping!"); } }; public abstract void execute(); private static ActivityService activityService; public void setActivityService(ActivityService activityService) { this.activityService = activityService; } } @Autowired ActivityService activityService; @Override public void executeMe(){ MyEnum myEnum = MyEnum.WALK; myEnum.setActivityService(activityService); myEnum.execute(); } } ```