这是我的旧代码,我决定重构它以使垃圾更容易,但现在我想知道它是否可以做得更简单。
public class AccountConstraint {
private Range<Integer> accountId;
private String username;
private String password;
private String email;
public AccountConstraint(Object accountId, final String username, final String password, final String email) {
if (accountId instanceof Integer) {
accountId = new Range<>(accountId);
}
this.accountId = (Range<Integer>)accountId;
this.username = username;
this.password = password;
this.email = email;
}
}
public class Range<T> {
private T min;
private T max;
public Range(T min, T max) {
this.min = min;
this.max = max;
}
public Range(T value) {
this.min = value;
this.max = value;
}
public T getMin() {
return min;
}
public T getMax() {
return max;
}
@Override
public String toString() {
return "[" + min + ", " + max + "]";
}
}
有了这个,您可以创建一个 AccountConstraint,它的 accountId 可以是 anInteger
或 a Range<Integer>
,并确保它始终保存为 a Range<Integer>
。
这里的背景信息是,您应该能够只输入一个整数5
,然后它将自动存储Range(5) = Range(5, 5)
。
代码工作正常,但看起来有点乱。所以我想出了以下改进:
新的构造函数:
public AccountConstraint(Object accountId, final String username, final String password, final String email) {
this.accountId = Range.<Integer>getRange(accountId, Integer.class);
this.username = username;
this.password = password;
this.email = email;
}
新方法:
public static <T> Range<T> getRange(Object object, Class<? extends T> clazz) {
if (clazz.isInstance(object)) {
object = new Range<>((T)object);
}
return (Range<T>)object;
}
它也有效,我认为它似乎更好,因为它现在可以作为单线完成,但它甚至可以做得更好吗?更具体地说,可以Class<? exends T>
省略吗?
还有一个小问题:应该是Class<T> clazz
还是Class? extends T> clazz
?
问候。
编辑:按照建议实现了构建器模式,因为有可能会有更多的参数可以重载。新代码:
package dao.constraint;
public class AccountConstraint {
private Range<Integer> accountId;
private String username;
private String password;
private String email;
private AccountConstraint(Builder builder) {
this.accountId = builder.accountId;
this.username = builder.username;
this.password = builder.password;
this.email = builder.email;
}
public Range<Integer> getAccountId() {
return accountId;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public String getEmail() {
return email;
}
public static class Builder {
private Range<Integer> accountId;
private String username;
private String password;
private String email;
public Builder() {
this.accountId = null;
this.username = null;
this.password = null;
this.email = null;
}
public Builder accountId(final int accountId) {
this.accountId = new Range<>(accountId);
return this;
}
public Builder accountId(final Range<Integer> accountId) {
this.accountId = accountId;
return this;
}
public Builder username(final String username) {
this.username = username;
return this;
}
public Builder password(final String password) {
this.password = password;
return this;
}
public Builder email(final String email) {
this.email = email;
return this;
}
public AccountConstraint build() {
return new AccountConstraint(this);
}
}
}
现在一个额外的好功能是不需要填写非强制性字段,因为默认值null
无论如何都是针对它们的。