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

bbbc7206899f383d9649df81e7711ee9.png

太多的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 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 invokable =

(Invokable) Invokable.from(method);

invokable.invoke(new ReflectTest());

}

3. lambda表达式

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

@Test

public void testJava8() {

Map> 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内容请搜索云海天教程以前的文章或继续浏览下面的相关文章希望大家以后多多支持云海天教程!

原文链接:https://blog.csdn.net/j16421881/article/details/79967948

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值