简体   繁体   English

确保不可变列表中可变对象的不变性

[英]Ensuring immutability for mutable objects in immutable list

I am required to implement an Immutable List interface and modify its methods to ensure immutability of a list.我需要实现一个不可变列表接口并修改其方法以确保列表的不变性。

I think I have managed to do so, but having trouble ensuring such immutability of mutable objects that are found in this list.我想我已经设法做到了,但是无法确保在此列表中找到的可变对象的这种不变性。

Consider the below interface:考虑以下接口:

public interface ImmutableListInterface<T>{

        T get(int index);

}

and its implementation:及其实施:

public final class ImmutableList<T> implements ImmutableListInterface<T>{

    private final List<T> immutableList; 

    public ImmutableList(List<T> list) {
        immutableList = list;
    }

    @Override
    public T get(int index) {

        T item;
        List<T> temp = new ArrayList<>(immutableList);

        try {
            //Try retrieving item
            item = temp.get(index);

        }catch(Exception e) {
            System.out.println("Error message: " + e.getMessage());
            return null; 
        }

        return item;
    }
}

Now if I were to initialise an ImmutableList of type MutableObject, this does not prevent me from modifying a property of MutableObject.现在,如果我要初始化 MutableObject 类型的 ImmutableList,这并不妨碍我修改 MutableObject 的属性。 As in:如:

 class MutableObject{
    public int a;
}
        LinkedList<MutableObject> list = new LinkedList<MutableObject>();

        MutableObject object = new MutableObject();
        object.a = 0;

        list.add(object);

        ImmutableList<MutableObject> immutable_list = new ImmutableList<MutableObject>(list);

        System.out.println("Before:" +  immutable_list.get(0).a); //prints 0, as expected

        immutable_list.get(0).a = 1;
        System.out.println("After:" + immutable_list.get(0).a);//prints 1 - expecting 0

I have tried setting the method get to final, but to no avail.我尝试将方法设置为最终,但无济于事。

It seems like I might have overlooked something in my implementation.似乎我可能忽略了我的实现中的某些内容。 How can I truly ensure immutability of the list, allowing the object itself to stay Mutable?我怎样才能真正确保列表的不变性,让对象本身保持可变?

The reason of your problem is that the object returned by get() method is referencing the same object in your actual list.问题的原因是get()方法返回的对象引用了实际列表中的同一个对象。 Therefore the changes made to it are applied to your actual list's object.因此,对其所做的更改将应用​​于实际列表的对象。

On the other hand, you cannot ensure immutability of list's contents, you can only ensure that their references are not modified, whereas their values may change.另一方面,你不能保证列表内容的不变性,你只能保证它们的引用不被修改,而它们的值可能会改变。

If you truly want to preserve objects in list and avoid modifying list's contents after get() , I suggest to return a deep copy of an object in your method.如果您真的想保留列表中的对象并避免在get()之后修改列表的内容,我建议在您的方法中返回一个对象的深层副本 It will return the same object with brand new reference, which will not be linked to your list.它将返回具有全新引用的相同对象,该对象不会链接到您的列表。

There are several ways to do it, you can find more information in this question有几种方法可以做到这一点,您可以在此问题中找到更多信息

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

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