I am looking to find the best implementation that would basically be a singleton strategy delegate design pattern. What would be the best/cleanest way to create singleton functionals in Java? I want to prevent unnecessary instance creation. I also want to make it so I can look up the correct function without doing a switch or having multiple if statements.I also don’t want to use reflection. Here are a couple different approaches that I found:
public interface Executable extends Serializable{ void execute(); } public class ActionA implements Executable{ ActionA(){ } void execute(){ //Do Action A } } public class ActionB implements Executable{ ActionB(){ } void execute(){ //Do Action B } } public enum Action{ ACTION_A(new ActionA()), ACTION_B(new ActionB()); private executable; private Action(Executable e){ executable =e; } public Executable getExecutable(){ return e; } } public class Service{ public static doService(Action input){ //preAction action.execute(); //postAction } } public class AlternateServiceImplementation(){ //Action no longer would contain the executable instances private static Map<> actionMap = new EnumMap<Action,Executable>; static{ actionMap.put(ACTION_A,new ActionA()); actionMap.put(ACTION_B,new ActionB()); } public static doService(Action action){ //preAction Executable executable=actionMap.get(action); executable.execute(); //postAction } }