下面是我的界面 -
public interface IDBClient {
public String read(ClientInput input);
}
这是我对接口的实现 -
public class DatabaseClient implements IDBClient {
@Override
public String read(ClientInput input) {
}
}
现在我有一个工厂获取实例 DatabaseClient
像这样 -
IDBClient client = DatabaseClientFactory.getInstance();
....
现在我需要调用 read
我的方法 DatabaseClient
它接受 ClientInput
参数,下面是相同的类。这门课不是我写的,所以这就是我对此有疑问的原因,我很确定这是错误的做法。
public final class ClientInput {
private Long userid;
private Long clientid;
private Long timeout_ms = 20L;
private boolean debug;
private Map<String, String> parameterMap;
public ClientInput(Long userid, Long clientid, Map<String, String> parameterMap, Long timeout_ms, boolean debug) {
this.userid = userid;
this.clientid = clientid;
this.parameterMap = parameterMap;
this.timeout_ms = timeout_ms;
this.debug = debug;
}
}
因此,当客户调用 read
方法 DatabaseClient
时,他们将创建 ClientInput
参数来获取实例的工厂 DatabaseClient
然后相应地调用读取方法。
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("attribute", "segmentation");
ClientInput input = new ClientInput(109739281L, 20L, paramMap, 1000L, true);
IDBClient client = DatabaseClientFactory.getInstance();
client.read(input);
问题陈述:-
- So my first question is does the
userid
,clientid
,timeout_ms
should beLong
object or just simplylong
在ClientInput
类? - 我的第二个问题是,客户可能会传递错误的信息,例如
negative user ids
,negative client id
, —79abbd7460d5fcf9433387a816f00741— ,negative timeout
做这个验证?我应该在ClientInput
类的构造函数中还是在其他地方进行验证检查?这样做的更好方法是什么?我应该如何进行验证?
原文由 AKIWEB 发布,翻译遵循 CC BY-SA 4.0 许可协议
我认为没有一个正确的答案。几点建议:
The biggest difference I see between
long
andLong
in this context is thatLong
may benull
.如果您可能有缺失值,Long
对象会有所帮助,因为null
可以指示缺失值。如果您使用基元,则必须使用一些特殊值来指示缺失,这可能会变得一团糟。速度或大小不太可能成为问题,除非您计划制作一百万个这样的东西然后序列化。我对验证逻辑的偏好是在可能失败的地方抛出某种自定义
ValidationException
。如果你只是用构造函数创建这些东西,最简单的事情就是在那里验证,例如最终,
ValidationException
仅当您可以在可以用它做一些有用的事情时抓住它才有用 - 将它回显给用户或其他任何东西。