简体   繁体   English

如何使用 Date 对象使类不可变?

[英]How to make a class immutable with Date object in it?

This is just from an academic learning point of view.这只是从学术学习的角度来看。 All I know is that whenever we want to make some class immutable, - it has to consist of final primitive fields - reference does not escapes during construction of object - if uses other objects, then those objects need to be recursively immutable too or API immutable classes like java.lang.String, among some other detailed lookouts!我所知道的是,每当我们想让某个类不可变时——它必须由最终的原始字段组成——在对象的构造过程中引用不会转义——如果使用其他对象,那么这些对象也需要递归不可变或 API 不可变像 java.lang.String 这样的类,以及其他一些详细的监视!

But I recently came accross a question wherein an interviewer asked a candidate to create an immutable class that has a java.util.Date in it.但我最近遇到一个问题,面试官要求应聘者创建一个不可变的类,其中包含 java.util.Date。 My first impression tells that its not possible although we can do workarounds with String containing the date string rather than in Date object itself.我的第一印象告诉这是不可能的,尽管我们可以使用包含日期字符串而不是 Date 对象本身的 String 来解决。

Please clarify me on this.请澄清我这一点。 Thank you.谢谢你。

The simplest thing to do here to make the class immutable is to create a defensive copy of the Date object (when it is passed in the construction parameters).在这里使类不可变的最简单的方法是创建 Date 对象的防御性副本(当它在构造参数中传递时)。 Then don't provide any setters as well.然后也不要提供任何 setter。 Like this no reference to the Date field in the class is visible to code outside this class and thus the Date can't be modified.像这样,对类中的 Date 字段的引用对于此类外部的代码是不可见的,因此无法修改 Date。

See Tom's comment for required getter characteristic!请参阅 Tom 对所需 getter 特性的评论! Thanks for the addition.谢谢你的补充。

(Getter should return a copy of the date field as well, since Date itself is mutable, and changing the returned field from the getter will change the class's field as well.) (Getter 也应该返回 date 字段的副本,因为 Date 本身是可变的,并且更改 getter 返回的字段也会更改类的字段。)

For more information and details: http://www.informit.com/articles/article.aspx?p=31551&seqNum=2有关更多信息和详细信息: http : //www.informit.com/articles/article.aspx?p= 31551& seqNum=2

I suggest to create a wrapper class around the date which you are using and dont provide any setters or any method which can actually change the value.我建议在您使用的日期周围创建一个包装类,并且不要提供任何可以实际更改值的设置器或任何方法。

For making it immutable you need to consider following things :为了使其不可变,您需要考虑以下事项:

  1. You need to make sure that the class cannot be overridden, - make it as final.您需要确保该类不能被覆盖, - 使其成为最终的。

  2. Make all the fields as private and final.将所有字段设为私有和最终字段。

  3. Dont provide any setters or any method which changes the instance variable.不要提供任何改变实例变量的设置器或任何方法。

  4. Defensively copying the objects between callee and caller.在被调用者和调用者之间防御性地复制对象。

    Consider this tutorial for more考虑本教程了解更多

1) Don't provide “setter” methods. 1) 不要提供“setter”方法。

2) Make all fields final and private 2) 将所有字段设为 final 和 private

3) Don't allow subclasses to override methods - Declare class as final 3) 不允许子类覆盖方法 - 将类声明为 final

4) For mutable instance variables - Ex date: In this case a special attention to be given. 4) 对于可变实例变量 - Ex date:在这种情况下要特别注意。

5) Make the constructor private and construct instances in factory methods. 5) 使构造函数私有并在工厂方法中构造实例。 Factory method to store object creation logic in single place.将对象创建逻辑存储在单个位置的工厂方法。

   public static MyImmutableClass createNewInstance(Integer fld1, String fld2, Date date)
{
    return new MyImmutableClass (fld1, fld2, date);
}

6) Instead a new Date object, with content copied to it, should be returned. 6) 相反,应该返回一个新的 Date 对象,其中复制了内容。

public Date getDateField() {
    return new Date(dateField.getTime());
}

- Here dateField is field which is set inside private constructor - 这里 dateField 是在私有构造函数中设置的字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM