gpt4 book ai didi

java - 在 Java 中创建输入验证方法的好习惯是什么?

转载 作者:行者123 更新时间:2023-11-29 06:43:38 24 4
gpt4 key购买 nike

如果我想验证我的输入,我应该将验证代码作为私有(private)辅助方法还是创建一个单独的静态辅助类?验证码是否会增加对象的大小?

更多信息

假设我有一个类

import java.util.Vector;


public class Place {
private final double longitude;
private final double latitude;
private final String id;

private String address;
private String name;
private String types;
private String icon;
private String phoneNumber;
private String websiteUrl;
private int rating;
private Vector<Integer> challenges;

public static class Builder {
// required parameter
private final double longitude;
private final double latitude;
private final String id;
// optional parameter
private String address = "n/a";
private String name = "n/a";
private String icon = "n/a";
private String phoneNumber = "n/a";
private String websiteUrl = "n/a";
private String types = "n/a";
private Vector<Integer> challenges = new Vector<Integer>();
private int rating = 0;

public Builder(double longitude, double latitude, String id) {
assert(longitude >= -180.0 && longitude <= 180.0);
assert(latitude >= -90.0 && longitude <= 90.0);
this.longitude = longitude;
this.latitude = latitude;
this.id = id;
}

public Builder address(String address) {
this.address = address;
return this;
}

public Builder types(String types) {
this.types = types;
return this;
}

public Builder name(String name) {
this.name = name;
return this;
}

public Builder icon(String icon) {
this.icon = icon;
return this;
}

public Builder phoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
return this;
}

public Builder websiteUrl(String websiteUrl) {
this.websiteUrl = websiteUrl;
return this;
}

public Builder builder(int rating) {
this.rating = rating;
return this;
}

public Place build() {
return new Place(this);
}
}

public Place(Builder builder) {
// required parameters
longitude = builder.longitude;
latitude = builder.latitude;
id = builder.id;

// optional parameters
address = builder.address;
types = builder.types;
name = builder.name;
icon = builder.icon;
phoneNumber = builder.phoneNumber;
websiteUrl = builder.websiteUrl;
rating = builder.rating;
challenges = builder.challenges;
}

public double getLongitude() {
return longitude;
}

public double getLatitude() {
return latitude;
}

public String getId() {
return id;
}

public void setAddress(String address) {
this.address = address;
}

public String getAddress() {
return address;
}

public String getTypes() {
return types;
}

public void setTypes(String types) {
this.types = types;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setIconUrl(String icon) {
this.icon = icon;
}

public String getIcon() {
return icon;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getPhoneNumber() {
return phoneNumber;
}


public void setWebsiteUrl(String websiteUrl) {
this.websiteUrl = websiteUrl;
}

public String getWebsiteUrl() {
return websiteUrl;
}

public void setRating(int rating) {
this.rating = rating;
}

public int getRating() {
return rating;
}

@Override
public String toString() {
return "(" + Double.toString(longitude) + ", " + Double.toString(latitude) + ")";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Place other = (Place) obj;
if (id == null) {
if (other.id != null)
return false;
}
else if (!id.equals(other.id))
return false;
return true;
}

public Vector<Integer> getChallenges() {
return new Vector<Integer>(challenges);
}

public void addChallenges(Integer i) {
this.challenges.add(i);
}

public void showChallenges() {
for (Integer i : challenges) {
System.out.print(i + ", ");
}
}
}

如果我必须在设置之前验证 address 参数,在这种情况下我应该将验证地址的代码放在哪里?

最佳答案

如果您只是想查看输入的字符串格式是否正确或长度是否正确,那么您可以使用私有(private)方法。另一方面,如果您要检查地址是否正确(在 map 上查找)或任何更高级的东西,那么创建一个 AddressValidator 接口(interface)并从该私有(private)方法调用它是有意义的。

私有(private)方法的原因是您可以从构造函数、setter 或任何其他可以提供地址的方法中调用它。界面的原因是您可能想要例如在线/离线 AddressValidator(MockAddressValidator,或为每个国家/地区调用不同类的类等)。

由于 AddressValidator 可以在其他类中重复使用,并且为了保持您的代码整洁,我会将其创建为顶级接口(interface) + OnlineAddressValidator。这也使您的类(class)更具可读性。对于完整的可配置性,您可能需要考虑如何提供 AddressValidator 实例,例如通过构造函数或定义为静态最终 validator 的 validator 。

public interface AddressValidator {
static class AddressValidatorResult {
// some results, you might want to return some useful feedback (if not valid)

boolean isValid() {
throw new IllegalStateException("Method not implemented yet");
}
}

public static class AddressValidationException extends Exception {
private AddressValidationException(AddressValidatorResult result) {
// add some implementation
}
}


// don't throw ValidateException here, invalid addresses are normal for
// validators, even if they aren't for the application that uses them
AddressValidatorResult validateAddress(String address);
// don't throw ValidateException here, invalid addresses are normal for
// validators, even if they aren't for the application that uses them
}

public class DefaultAddressValidator implements AddressValidator {

public static class Params {
// some parameters for this specific validator
}

private final Params params;

public DefaultAddressValidator(Params params) {
// creates this validator
this.params = params;
}

@Override
public AddressValidatorResult validateAddress(String address) {
// perform your code here

// I don't like "return null" as it may lead to bugs
throw new IllegalStateException("Method not implemented yet");
}
}


// and use it like this
private void validateAddress(String address) throws AddressValidationException {
// e.g. field AddressValidator set in constructor
AddressValidatorResult result = addressValidator.validateAddress(address);
if (!result.isValid()) {
throw new AddressValidationException(result);
}
}

关于java - 在 Java 中创建输入验证方法的好习惯是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8621344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com