-3

I have a problem with my enum. I need to call different functions based on enum values I can do it based on switch case but it is not a solution in my case sinse I have approx 50 values. So what I am trying to do is pass each function in enum constructor with dynamic runtime values.Below is some example code snippet to make it more clear.

public enum TestEnum {

TEST_ENUM1("1", TestUtil.one(name)),
TEST_ENUM2("2", TestUtil.two(name));

private String display;
private Runnable function;
private static String name;
TestEnum(String number,Runnable function){
    this.display = number;
    this.function = function;
}

public String getDisplay() {
    return display;
}

public Runnable getFunction() {
    return function;
}

}

and to execute this I need to iterate over thin enum and call each method with dynamic value for name field.

      for (TestEnum enumv : TestEnum.values()) {
        enumv.getFunstion.execute();
        }

Is it possible to achieve the same if yes then please let me know the best way to do it. Thanks

freak007
  • 88
  • 9
  • I don't understand what your question is. What is your code supposed to do, and what does it do instead? – JB Nizet Dec 25 '16 at 09:53
  • don't pass runnable if you want runtime values, create your own interface that accepts a param in execute method – AdamSkywalker Dec 25 '16 at 09:54
  • You can you polymorphism in this case. Take a look [here](http://stackoverflow.com/a/126455/6348498). It'll be easier to maintain and fulfill what you require – Gurwinder Singh Dec 25 '16 at 09:54
  • Have a look at Bloch, "Effective Java" Item 30 for ideas. – rossum Dec 25 '16 at 10:05
  • What is `name` supposed to represent in this code: `TEST_ENUM1("1", TestUtil.one(name))`? Is your `Runnable` actually a `Function`? – Bohemian Dec 25 '16 at 10:12
  • I just want to pass this name ans parameter so it can be any string value. I have written for example only to show what I am trying to acheive. In face i have to pass a httpservlet request in it. @Bohemian – freak007 Dec 25 '16 at 10:13
  • Do you mean that you want to pass the name of the enum? – Bohemian Dec 25 '16 at 10:36

3 Answers3

1

You can declare an abstract method at enum and then let each subsequent enum implement given method. When you're iterating over your enums your each enum is going to execute it's own implementation.

 public enum MyEnum {

    FirstEnum {
        public void myMethod(String args) {
        }
    },
    SecondEnum {
        public void myMethod(String args) {
        }
    };

    public abstract void myMethod(String args);

}

This way you avoid having numerous if -else statements and switch-case statements and you increase readability by a large margin.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
Pavisa
  • 102
  • 7
0

You can do this with the command pattern, although I'm not sure that it will be less verbose than using a switch.

public class Main {

    public static void main(String[] args) {
        TestEnum.ONE.execute();
        TestEnum.TWO.execute();
    }
}

enum TestEnum {

    ONE("1", new Command1()),
    TWO("2", new Command2());

    String name;
    Command command;

    TestEnum(String name, Command command) {
        this.name = name;
        this.command = command;
    }

    public void execute() {
        command.execute(name);
    }
}

interface Command {

    void execute(String name);
}

class Command1 implements Command {

    @Override
    public void execute(String name) {
        System.out.println("Command1: " + name);
    }
}

class Command2 implements Command {

    @Override
    public void execute(String name) {
        System.out.println("Command2: " + name);
    }
}
Riaan Nel
  • 2,425
  • 11
  • 18
0

As @Pavisa said, you can define abstract method and implement for each enum constant.

In that case, this works:

for (TestEnum enumv : TestEnum.values()) {
    enumv.getFunstion.execute();
}

But it is harder to maintain.

You can use polymorphism to attain this more flexibly:

public interface Action {
    void doStuff();
}

then, create what functionality you want by implementing Action or extending existing implementation of it to reuse the code.

For interating and running it, you can do:

List<Action> actions = getActions();
for(Action action : actions) {
    action.doStuff();
}
Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76