zoukankan      html  css  js  c++  java
  • 响应式编程

    响应式编程(reactive programming)是一种基于数据流(data stream)和变化传递(propagation of change)的声明式(declarative)的编程范式
    在命令式编程(我们的日常编程模式)下,式子a=b+c,这就意味着a的值是由b和c计算出来的。如果b或者c后续有变化,不会影响到a的值
    在响应式编程下,式子a:=b+c,这就意味着a的值是由b和c计算出来的。但如果b或者c的值后续有变化,会影响到a的值
    我认为上面的例子已经可以帮助我们理解变化传递(propagation of change)

    lambda:

    函数式编程接口:
    @FunctionalInterface



    其中在函数式接口中有一些只适合在函数式中使用的官方封装好的接口方法
    如BiFunction,Tuple
    BiFunction适用于两个参数,而Function适用于一个参数
    Function的使用:

    
        /**
         * 一个参数的调用返回
         *
         * @param option
         * @param funOptionService
         * @return
         */
        public static Mono<ServerResponse> oneOptionalParams(Optional<String> option,
                                                             Function<Optional<String>, Mono<JSONObject>> funOptionService,
                                                             String judgeParam) {
            //option is not Present and the judgeParams not null then badRequest
            if (!option.isPresent() && (null != judgeParam) && judgeParam != "") {
                return (Mono<ServerResponse>) ServerResponseBuild.badRequestMsg("参数" + judgeParam + "不能为空");
            }
            Mono<JSONObject> jsonObjectMono = funOptionService.apply(option);
            return jsonObjectMono.flatMap(
                    resultJson -> {
                        if (resultJson.getInteger("errcode") != 0) {
                            return ServerResponseBuild.badRequestMsg(resultJson.getString("errmsg"));
                        }
                        log.info("
    返回数据:{}", resultJson);
                        return ServerResponseBuild.okRequestObj(resultJson, JSONObject.class);
                    }
            ).switchIfEmpty(ServerResponseBuild.badRequestMsg("请求错误")
            ).onErrorResume(error -> ServerResponseBuild.badRequestMsg(error.getMessage()));
        }
    

    BiFunction两个参数的

        /**
         * 两个参数的调用返回
         * @param option1
         * @param option2
         * @param funBiOptionService
         * @param judgeParams
         * @return
         */
        public static Mono<ServerResponse> twoOptionalParams(Optional<String> option1, Optional<String> option2,
                                                             BiFunction<Optional<String>, Optional<String>, Mono<JSONObject>> funBiOptionService,
                                                             String... judgeParams) {
            //省略judgeParams...
            Mono<JSONObject> jsonObjectMono = null;
            try {
                jsonObjectMono = funBiOptionService.apply(option1, option2);
            } catch (Exception e) {
                return Mono.error(e);
            }
            return jsonObjectMono.flatMap(
                    resultJson -> {
                        if (resultJson.getInteger("errcode") != 0) {
                            return ServerResponseBuild.badRequestMsg(resultJson.getString("errmsg"));
                        }
                        log.info("
    返回数据:{}", resultJson);
                        return ServerResponseBuild.okRequestObj(resultJson, JSONObject.class);
                    }
            ).switchIfEmpty(ServerResponseBuild.badRequestMsg("请求错误")
            ).onErrorResume(error -> ServerResponseBuild.badRequestMsg(error.getMessage()));
        }
    

    函数式调用

    这样写起来确实看上去简洁了许多
    且这样调用可以调用接收多个参数
    一个参数:

        /**
         * 获取访问用户身份
         *
         * @param request
         * @return
         */
        public Mono<ServerResponse> getuserInfo(ServerRequest request) {
            return ServerResponseBuild.oneOptionalParams(request.queryParam("code"),
                    option -> manIdentityVerifyService.getUserInfo(option),
                    "code");
        }
    

    上面的写法可以改为:

        /**
         * 获取访问用户身份
         *
         * @param request
         * @return
         */
        public Mono<ServerResponse> getuserInfo(ServerRequest request) {
            return ServerResponseBuild.oneOptionalParams(request.queryParam("code"),
                    manIdentityVerifyService::getUserInfo,
                    "code");
        }
    

    两个参数的同样可以使用::方式来调用方法

        /**
         * ~~~~不给你看注释
         * @return
         */
        public Mono<ServerResponse> getConversations(ServerRequest request) {
            return ServerResponseBuild.twoOptionalParams(
                    request.queryParam("seq"),
                    request.queryParam("limit"),
                    chatMsgSaveService::monoGetChatContent,
                    "seq", "limit");
        }
    
  • 相关阅读:
    linux 定时备份数据库
    Web前端优化>javascript优化
    IT项目开发的75条管理守则
    Web前端优化>css 优化
    Web前端优化>图象篇
    SNS关键点
    项目管理(对事不对人)
    Yahoo!网站性能最佳体验的34条黄金守则——内容
    互联网主题知名博客
    Web前端优化>Cookie 优化
  • 原文地址:https://www.cnblogs.com/ukzq/p/14023176.html
Copyright © 2011-2022 走看看