4

My code looks like below:

enum EnumType {
 CATEGORY,
 GROUP,
 MAIN
}

Methods:

public void call(EnumType type){
   switch(type):
     case CATEGORY:
        return methodForCategory();
     case GROUP:
        return methodForGroup();
     ...
}
public void methodForCategory(){
   ... Operations according to EnumType.CATEGORY
}
public void methodForGroup(){
   ... Operations according to EnumType.GROUP
}
public void methodForMain(){
   ... Operations according to EnumType.MAIN
}

But I want to call it without switch/case like below;

public void call(EnumType type){
    methodForType(EnumType type);
}

Is it possible or is there any better alternative?

1
  • 1
    Common approaches are to have actual classes, and override the methods (if you want them structured see composite pattern). If you want to have the methods separate from the hiearchy, double dispatch (or visitor pattern)
    – k5_
    Commented Sep 14, 2019 at 7:54

2 Answers 2

5

You can create the method implementation inside the enum as below:

public enum EnumType {

  CATEGORY {

    @Override
    public void processMethod() {
      // Do something here
    }

  },
GROUP {

    @Override
    public void processMethod() {
      // Do something here
    }

  },
MAIN {

    @Override
    public void processMethod() {
      // Do something here
    }

  };

public abstract void processMethod();
}

And update call method implementation as:

public void call(EnumType type){
   type.processMethod();
}

And switch code should not return anything as method return type is void.

2
  • this solution can be used for basic static operations (for processMethod implementation). But I think that it is not effective solution for external dynamic operations (call method from other classes). Because it will be complex for an enum class?
    – Sha
    Commented Sep 14, 2019 at 8:29
  • The you can use Strategy pattern for it. Where the above approach will give the object of Strategy class. Commented Sep 14, 2019 at 15:09
1

You can use an EnumMap as a registry of methods and using the Enum supplied you can return the correct implementation of the Runnable. I have used Runnable as a functional interface as it takes no inputs and produces no output.

In another class where you have the business logic, you can initialize the map and add the corresponding Runnable implementation:

class Strategy{

    private final EnumMap<EnumType, Runnable> map;

    public Strategy(){
        //Initialize values here
        map = new EnumMap<>(EnumType.class);
        map.put(EnumType.CATEGORY, () -> {
            System.out.println("CATEGORY");
        });
        map.put(EnumType.GROUP, () -> {
            System.out.println("GROUP");
        });
        map.put(EnumType.MAIN, () -> {
            System.out.println("MAIN");
        });
    }

    public void call(EnumType type){
        map.get(type).run();
    }
}

Then you can invoke the call() method by supplying the type of Enum as a parameter:

public static void main(String args[]){
    Strategy str = new Strategy();
    str.call(EnumType.CATEGORY);
    str.call(EnumType.GROUP);
    str.call(EnumType.MAIN);
}
2
  • Actually you could make the value part of the EnumMap any functional interface, couldn't you?
    – Abra
    Commented Sep 14, 2019 at 8:24
  • @Abra That will depend on the use case, here OP needs a method which takes no input or return no output for that I have used a Runnable, if OP needed to return something without taking any input I would've gone for a Supplier Commented Sep 14, 2019 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.