Java中避免过多if-else的几种方法
作者:tom3mao 时间:2023-11-28 13:07:09
太多的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();
}
来源:https://blog.csdn.net/j16421881/article/details/79967948
标签:Java,if-else
0
投稿
猜你喜欢
Java(TM) Platform SE binary 打开jar文件的操作
2021-10-02 00:08:12
Android自定义TitleView标题开发实例
2023-09-05 18:21:41
SpringMVC 数据校验方法(必看篇)
2023-11-14 21:44:05
Android中TextureView与SurfaceView用法区别总结
2023-07-20 00:29:16
Java Spring AOP之PointCut案例详解
2023-05-24 16:46:15
Android使用kotlin实现多行文本上下滚动播放
2022-05-09 08:08:29
IntelliJ IDEA快速创建getter和setter方法
2023-06-04 00:44:46
Java使用Iterator迭代器遍历集合数据的方法小结
2021-10-28 00:22:40
程序猿必须要掌握的多线程安全问题之锁策略详解
2021-10-19 04:57:17
c#高效的线程安全队列ConcurrentQueue<T>的实现
2021-07-27 11:01:05
SpringBoot全局配置long转String丢失精度的问题解决
2023-02-19 22:58:49
在Ubuntu中安装VSCode并配置C/C++开发环境的方法步骤
2021-07-04 18:19:19
Android实现计步传感器功能
2021-09-29 21:54:41
关于idea中SpringBoot启动失败的坑
2022-07-18 13:02:24
springboot配置aop切面日志打印过程解析
2022-07-10 13:14:09
Android 嵌套Fragment的使用实例代码
2022-07-18 06:37:15
Android仿微信activity滑动关闭效果
2022-12-05 15:01:37
c# 实现雪花分形的示例
2023-05-10 02:59:56
C#遍历操作系统下所有驱动器的方法
2022-06-29 09:12:14
Java SpringMVC异步处理详解
2021-08-10 15:03:58