Best practice for returning multiple values in Java?(在 Java 中返回多个值的最佳实践?)
问题描述
今天,我在登录表单后面添加了额外的安全检查,以减缓暴力攻击.我有多个登录表单,并制作了一个易于调用的函数,该函数执行所有检查,然后返回结果.
Today I've added a extra security check behind my login forms, to slow down brute force attacks. I've got multiple login forms and made a nice easy to call function that does all the checking and then returns the result.
public static ValidateLoginResult validateLogin(HttpServletRequest request, String email, String password) {
问题是结果不是单个值,结果包括:
The problem is the result is not a single value, the result consists of:
boolean ok
String errorMessage
boolean displayCaptcha
为此,我创建了一个新类.这一切都很好.
For this I created a new class. This all works fine.
但我经常有方便的实用函数返回多个值,并且开始发现每次为结果创建一个新类有点烦人.
But I often have handy utility functions that return multiple values and start to find it a bit annoying to create a new class for the result every time.
有没有更好的方法来返回多个值?还是我只是懒惰?:)
Is there a better way of returning multiple values? Or am I just lazy? :)
推荐答案
不,这种结构在Java中是天生不存在的,但是你可以看看JavaTuples 库 可能适合您的需要并提供非常优雅的解决方案.使用 Triplet<Boolean, String, Boolean>
No, this kind of structure doesn't exists nativily in Java, but you can look at JavaTuples library that may suit your need and provide a quite elegant solution. Using a Triplet<Boolean, String, Boolean>
这篇关于在 Java 中返回多个值的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Java 中返回多个值的最佳实践?
基础教程推荐
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
