Java函数式编程最佳实践

别人说烂了的stream api不就不想赘述了,我想和大家分享一下,如何用函数式编程来简化我们的开发,想说点不一样的东西
转载链接

简化事务

对于事务而言,应该粒度越小越好,并且读写逻辑应该分开,只在写的逻辑上执行事务,可以用函数式编程来简化抽去写逻辑这一步

java 复制代码
@Service
public class TransactionService {
    @Transactional
    public void process(ThrowExceptionRunnable runnable){
        try {
            runnable.run();
        }catch (Exception e){
            new RuntimeException(e);
        }
    }
}

//使用方式
public void regist(String username){
	User user = userService.findByUserName(username);
  	if(user != null) return;
  	
  	//执行事务 注册用户 开通余额账号
  	transactionService.process(() -> {
      	userService.save(new User(username));
        balanceService.save(new Balance(username));
    });
}

赋予方法重试能力

java 复制代码
public static void retryFunction(ThrowExceptionRunnable runnable, int time) {
        while (true) {
            try {
                runnable.run();
                return;
            } catch (Exception e) {
                time--;
                if (time <= 0) throw new RuntimeException(e);
            }
        }
    }
    public static <T, R> R retryFunction(ThrowExceptionFunction<T, R> function, T t, int time) {
        while (true) {
            try {
                return function.apply(t);
            } catch (Exception e) {
                time--;
                if (time <= 0) throw new RuntimeException(e);
            }
        }
    }
    public static <T, U, R> R retryFunction(ThrowExceptionBiFunction<T, U, R> function, T t, U u, int time) {
        while (true) {
            try {
                return function.apply(t, u);
            } catch (Exception e) {
                time--;
                if (time <= 0) throw new RuntimeException(e);
            }
        }
    }
    public static void main(String[] args) {
  	//http调用,失败会重试3次
     	retryFunction(()->http.call(),3);
  	//把数字1转成数字 失败会重试三次
  	String s = retryFunction(String::valueOf, 1, 3);
  	String ss = retryFunction(i -> String.valueOf(i), 1, 3);
    }

赋予函数缓存能力

java 复制代码
public static <T, R> R cacheFunction(Function<T, R> function, T t, Map<T, R> cache) {
    R r = cache.get(t);
    if (r != null) return r;
    R result = function.apply(t);
    cache.put(t,result);
    return result;
}

public static void main(String[] args) {
        Map<String,User> cache = new HashMap<Integer, User>();
        String username = "张三";
  			//不走缓存
        cacheFunction(u -> userService.findByUserName(u),username,cache);
  			//走缓存
        cacheFunction(u -> userService.findByUserName(u),username,cache);
    }

赋予函数报错返回默认值能力

java 复制代码
public static <T, R> R computeOrGetDefault(ThrowExceptionFunction<T, R> function, T t, R r) {
        try {
            return function.apply(t);
        } catch (Exception e) {
            return r;
        }
    }
    public static <R> R computeOrGetDefault(ThrowExceptionSupplier<R> supplier,R r){
        try {
            return supplier.get();
        } catch (Exception e) {
            return r;
        }
    }

	public static void main(String[] args) {
  	//返回0
  	computeOrGetDefault(i -> {
           if (i < 0) throw new RuntimeException();
           else return i;
        }, -1, 0);
  	//返回5
        computeOrGetDefault(i -> {
            if (i < 0) throw new RuntimeException();
            else return i;
        },5,0);
    }

赋予函数处理异常的能力

java 复制代码
public static <T, R> R computeAndDealException(ThrowExceptionFunction<T, R> function, T t, Function<Exception, R> dealFunc) {
        try {
            return function.apply(t);
        } catch (Exception e) {
            return dealFunc.apply(e);
        }
    }

    public static <T, U, R> R computeAndDealException(ThrowExceptionBiFunction<T,U, R> function, T t, U u,Function<Exception, R> dealFunc) {
        try {
            return function.apply(t,u);
        } catch (Exception e) {
            return dealFunc.apply(e);
        }
    }

    public static <R> R computeAndDealException(ThrowExceptionSupplier<R> supplier, Function<Exception, R> dealFunc) {
        try {
            return supplier.get();
        } catch (Exception e) {
            return dealFunc.apply(e);
        }
    }

public static void main(String[] args) {
    //返回异常message的hashcode
    Integer integer = computeAndDealException(i -> {
        if (i < 0) throw new RuntimeException("不能小于0");
        else return i;
    }, -1, e -> e.getMessage().hashCode());
    System.out.println(integer);
  
}

赋予函数记录日志能力

java 复制代码
public static <T, R> R logFunction(Function<T, R> function, T t, String logTitle) {
    long startTime = System.currentTimeMillis();
    log.info("[[title={}]],request={},requestTime={}", logTitle, t.toString(),
    	    LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    R apply = function.apply(t);
    long endTime = System.currentTimeMillis();
    log.info("[[title={}]],response={},spendTime={}ms", logTitle, apply.toString(), endTime - startTime);
    return apply;
}

public static void main(String[] args) {
    logFunction(String::valueOf,"s","String.valueOf");
}

自定义函数接口

java 复制代码
    @FunctionalInterface
    public interface ThrowExceptionFunction<T, R> {
        R apply(T t) throws Exception;
    }

    @FunctionalInterface
    public interface ThrowExceptionBiFunction<T, U, R> {
        R apply(T t, U u) throws Exception;
    }
    @FunctionalInterface
    public interface ThrowExceptionSupplier<T> {
        T get() throws Exception;
    }
    @FunctionalInterface
    public interface ThrowExceptionRunnable {
        void run() throws Exception;
    }

Q:为什么要自定义函数接口

A:自带的函数接口无法处理检查异常,遇见带检查异常的方法会报错

我哪些场景用到了?

链式取数

在翻译php代码的时候我们常常遇到如下情况

php 复制代码
$s = a.b.c.d.e.f.g

然后翻译成java代码的时候是这样的

java 复制代码
String s = a.getB().getC().getD().getE().getF().getG();

有啥问题?没有没有判空,只要中间有一层为空,那么就是NPE,要是去写判空逻辑的话,真是要了命了

这时我们就可以用上上面提到的骚操作了

代码改写

java 复制代码
String s = computeOrGetDefault(()->a.getB().getC().getD().getE().getF().getG(),"");
事务
简单的降级操作(computeAndDealException)
接口重试
接口缓存
记录日志
相关推荐
暖焰核心1 分钟前
继承全解——继承、默认成员函数、切片、隐藏与虚继承
java·前端·javascript
君顾110 分钟前
外卖CPS系统开发实战指南:从架构设计到部署全流程解析
java·开发语言·外卖
是立不是利1 小时前
前端交互基石:深入剖析JavaScript三级联动背后的设计哲学
开发语言·前端·javascript
奇牙coding1231 小时前
GPT-5.5 升级 GPT-5.6 接入指南:流式调用配置与常见问题
java·网络·gpt·ai
老刘的望远镜1 小时前
AgentScope Java 从零(03):Agent 的记性默认全开,我在第 50 轮翻了车
java·开发语言·javascript
Littlehero_1211 小时前
QT自定义控件之电气接线图(源码开源)
开发语言·qt
yxlalm1 小时前
5.3解决超卖问题
java·spring boot·超卖
中科三方1 小时前
DNS拨测到底在拨测什么?
开发语言·github·php·dns·云拨测
软件黑马王子1 小时前
20.Resources资源加载:基本实现
开发语言·前端框架·c#