Java中避免过多if-else的几种方法

2022-07-28,,

太多的if-else不太直观,难以维护。 

以下面代码为例,展示几种替代if else的方法。

string input = "three";

  @test
  public void testelse() {
    if ("one".equals(input)) {
      system.out.println("one");
    } else if ("two".equals(input)) {
      system.out.println("two");
    } else if ("three".equals(input)) {
      system.out.println("three");
    } else if ("four".equals(input)) {
      system.out.println("four");
    }
  } 

需要引入spring跟guava依赖

1.spring结合策略模式

spring可以将一组实现了同样接口的类注入一个list

/***
 * 定义接口。type用来路由具体的handler实现
 * */
public interface handler {
  string gettype();

  void execute();
}
 /**
   * 将handler接口的实现类注入一个list
   * */
  @autowired
  private list<handler> handlerlist;
  @test
  public void testautowirelist(){
  // 根据类型判断该由哪个具体实现类处理
     for(handler handler:handlerlist){
       if(input.equals(handler.gettype())){
         handler.execute();
       }
     }
  } 

下面是几种轻量级实现.

2. 反射

通过反射动态调用相应的方法

/***
 *定义每种类型所对应的方法
*/
public class reflecttest {
  public void methodone() {
    system.out.println("one");
  }

  public void methodtwo() {
    system.out.println("two");
  }

  public void methodthree() {
    system.out.println("three");
  }

  public void methodfour() {
    system.out.println("four");
  }

}

 /***
   *
   * 通过反射,动态调用方法。采用了guava的工具类。
   * */
  @test
  public void testreflect() throws exception {
    //首字母大写,根据类型拼接方法
    string methodname = "method" + lower_camel.to(upper_camel, input);
    method method = reflecttest.class.getdeclaredmethod(methodname);
    invokable<reflecttest, object> invokable =
        (invokable<reflecttest, object>) invokable.from(method);
    invokable.invoke(new reflecttest());
  } 

3. lambda表达式

实现同上面的反射,结合了java 8的新特性:lambda表达式

  @test
  public void testjava8() {
    map<string, consumer<reflecttest>> functionmap = maps.newhashmap();
    functionmap.put("one", reflecttest::methodone);
    functionmap.put("two", reflecttest::methodtwo);
    functionmap.put("three", reflecttest::methodthree);
    functionmap.put("four", reflecttest::methodthree);
    functionmap.get(input).accept(new reflecttest());
  } 

4. 枚举

在枚举里面定义一个抽象方法,每种类型对应各自的具体实现。

/**
 * 定义枚举类,包含了所有类型
 */
public enum enumtest {


  one("one") {
    @override
    public void apply() {
      system.out.println("one");
    }
  },
  two("two") {
    @override
    public void apply() {
      system.out.println("two");
    }
  }, three("three") {
    @override
    public void apply() {
      system.out.println("three");
    }
  }, four("four") {
    @override
    public void apply() {
      system.out.println("four");
    }
  };

  public abstract void apply();

  private string type;

  enumtest(string type) {
    this.type = type;
  }

  public string gettype() {
    return type;
  }

}
 // 枚举测试
 @test
  public void testenum() {
    enumtest.valueof(input.touppercase()).apply();
  }

到此这篇关于java中避免过多if-else的几种方法的文章就介绍到这了,更多相关java 过多if-else内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《Java中避免过多if-else的几种方法.doc》

下载本文的Word格式文档,以方便收藏与打印。