Java制作不可变子类

时间:2015-07-04 13:11:55

标签: java abstract-class immutability

我有以下抽象类:

    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;
        }
    }

和以下子类:

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;
    }
}

我需要创建一个子类,但是我想做一下制作不可变类的最佳实践。我将我的抽象构造函数设置为protected,声明我的变量private final / no setters。我的子类是不可变的吗?我做过一些糟糕的练习吗?如果是这样,我该如何解决?

1 个答案:

答案 0 :(得分:4)

您的类AnonymousCilent不是不可变的,因为类java.util.Date不是不可变的。我能做到这一点:

AnonymousCilent object = ...;

// Modifies the Date object held by the AnonymousCilent object
object.getArrivalDate().setDate(23);

为防止这种情况,Date对象的getter应该返回成员变量的副本,例如:

public Date getArrivalDate() {
    // Return a copy of member variable arrivalDate
    return new Date(arrivalDate.getTime());
}

但最好使用Java 8 java.time类,它们是不可变的,而不是java.util.Date,或者如果你不使用Java 8,则使用Joda Time