How Can I Create method In Java With Same Type Parameter?

前提是你 提交于 2020-06-14 06:25:45

问题


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:


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:


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);
}



回答3:


You can use a variant of the Command design pattern and store a reference to a function as a field of the enumerated type, and provide a common execute() method to execute the custom operation.

enum EnumType {
    CATEGORY( ()-> {} ), 
    GROUP( ()-> {System.out.println("Hello");} ),
    MAIN( EnumType::doSomething );

    private final Runnable operation;

    private EnumType(Runnable operation) {
        this.operation = operation;
    }

    public void execute() {
        operation.run();
    }

    private static final void doSomething() {
        System.out.println("World");
    }
}

If the methods are short, you can use lambda expression (as for GROUP). If they are longer, you can use method references (as for MAIN). If you need arguments or a return value, you can use a different functional interface.



来源:https://stackoverflow.com/questions/57933491/how-can-i-create-method-in-java-with-same-type-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!