Java 制作不可变的子类
Java Making Immutable Subclasses
我有以下摘要class:
public abstract class Client {
final private String anonymousCilentID;
final private String anonymousCilentFirstName;
final private String anonymousCilentLastName;
final private String gender;
protected Client(String anonymousCilentID, String anonymousCilentFirstName, String anonymousCilentLastName, String gender) {
this.anonymousCilentID = anonymousCilentID;
this.anonymousCilentFirstName = anonymousCilentFirstName;
this.anonymousCilentLastName = anonymousCilentLastName;
this.gender = gender;
}
public String getAnonymousCilentID() {
return anonymousCilentID;
}
public String getAnonymousCilentFirstName() {
return anonymousCilentFirstName;
}
public String getAnonymousCilentLastName() {
return anonymousCilentLastName;
}
public String getGender() {
return gender;
}
}
和以下子class:
final public class AnonymousCilent extends Client {
final private Date arrivalDate;
final private String immStatus;
final private Date registrationDate;
final private String registrationSite;
final private String siteName;
final private String comments;
final public class AnonymousCilent extends Client {
final private Date arrivalDate;
final private String immStatus;
final private Date registrationDate;
final private String registrationSite;
final private String siteName;
final private String comments;
public AnonymousCilent(String anonymousCilentID, String anonymousCilentFirstName, String anonymousCilentLastName, String gender, Date arrivalDate, String immStatus, Date registrationDate, String registrationSite, String siteName, String comments) {
super(anonymousCilentID,anonymousCilentFirstName, anonymousCilentLastName, gender);
this.arrivalDate = arrivalDate;
this.immStatus = immStatus;
this.registrationDate = registrationDate;
this.registrationSite = registrationSite;
this.siteName = siteName;
this.comments = comments;
}
public Date getArrivalDate() {
return new Date (arrivalDate.getTime());
}
public String getImmStatus() {
return immStatus;
}
public Date getRegistrationDate() {
return new Date (registrationDate.getTime());
}
public String getRegistrationSite() {
return registrationSite;
}
public String getSiteName() {
return siteName;
}
public String getComments() {
return comments;
}
}
我需要创建一个子class,但我想做的是制作不可变 classes 时的最佳实践。我将我的抽象构造函数设置为受保护,将我的变量声明为私有 final/no setter。我的 subclass 是不可变的吗?我做了一些不好的事情吗?如果是这样,我该如何解决?
你的classAnonymousCilent
不是一成不变的,因为classjava.util.Date
不是一成不变的。我可以这样做:
AnonymousCilent object = ...;
// Modifies the Date object held by the AnonymousCilent object
object.getArrivalDate().setDate(23);
为了防止这种情况,Date 对象的 getter 应该 return 成员变量的副本,例如:
public Date getArrivalDate() {
// Return a copy of member variable arrivalDate
return new Date(arrivalDate.getTime());
}
但是最好使用Java 8 java.time
classes,它们是不可变的,而不是java.util.Date
,或者使用Joda Time如果你没有使用 Java 8.
我有以下摘要class:
public abstract class Client {
final private String anonymousCilentID;
final private String anonymousCilentFirstName;
final private String anonymousCilentLastName;
final private String gender;
protected Client(String anonymousCilentID, String anonymousCilentFirstName, String anonymousCilentLastName, String gender) {
this.anonymousCilentID = anonymousCilentID;
this.anonymousCilentFirstName = anonymousCilentFirstName;
this.anonymousCilentLastName = anonymousCilentLastName;
this.gender = gender;
}
public String getAnonymousCilentID() {
return anonymousCilentID;
}
public String getAnonymousCilentFirstName() {
return anonymousCilentFirstName;
}
public String getAnonymousCilentLastName() {
return anonymousCilentLastName;
}
public String getGender() {
return gender;
}
}
和以下子class:
final public class AnonymousCilent extends Client {
final private Date arrivalDate;
final private String immStatus;
final private Date registrationDate;
final private String registrationSite;
final private String siteName;
final private String comments;
final public class AnonymousCilent extends Client {
final private Date arrivalDate;
final private String immStatus;
final private Date registrationDate;
final private String registrationSite;
final private String siteName;
final private String comments;
public AnonymousCilent(String anonymousCilentID, String anonymousCilentFirstName, String anonymousCilentLastName, String gender, Date arrivalDate, String immStatus, Date registrationDate, String registrationSite, String siteName, String comments) {
super(anonymousCilentID,anonymousCilentFirstName, anonymousCilentLastName, gender);
this.arrivalDate = arrivalDate;
this.immStatus = immStatus;
this.registrationDate = registrationDate;
this.registrationSite = registrationSite;
this.siteName = siteName;
this.comments = comments;
}
public Date getArrivalDate() {
return new Date (arrivalDate.getTime());
}
public String getImmStatus() {
return immStatus;
}
public Date getRegistrationDate() {
return new Date (registrationDate.getTime());
}
public String getRegistrationSite() {
return registrationSite;
}
public String getSiteName() {
return siteName;
}
public String getComments() {
return comments;
}
}
我需要创建一个子class,但我想做的是制作不可变 classes 时的最佳实践。我将我的抽象构造函数设置为受保护,将我的变量声明为私有 final/no setter。我的 subclass 是不可变的吗?我做了一些不好的事情吗?如果是这样,我该如何解决?
你的classAnonymousCilent
不是一成不变的,因为classjava.util.Date
不是一成不变的。我可以这样做:
AnonymousCilent object = ...;
// Modifies the Date object held by the AnonymousCilent object
object.getArrivalDate().setDate(23);
为了防止这种情况,Date 对象的 getter 应该 return 成员变量的副本,例如:
public Date getArrivalDate() {
// Return a copy of member variable arrivalDate
return new Date(arrivalDate.getTime());
}
但是最好使用Java 8 java.time
classes,它们是不可变的,而不是java.util.Date
,或者使用Joda Time如果你没有使用 Java 8.