不可变对象和不可修改的集合 [英] Immutable objects and unmodifiable collections

查看:159
本文介绍了不可变对象和不可修改的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用不可变对象,将包含的集合包装为不可修改是合适的?我看到3个选项:

With an immutable object, where is it proper to wrap a contained collection as unmodifiable? I see 3 options:


  1. 在immutable对象的工厂中:

  1. In the immutable object's factory:

public class ImmutableFactory {

    public Immutable build(){
       List<Integer> values = new ArrayList<Integer>();

        values.add(1);
        values.add(2);
        values.add(3);

        return new Immutable(Collections.unmodifiableList(values), "hello");
    }
}


  • 在不可变结构

  • In the immutable's contructor

    public class Immutable {
    
        private final List<Integer> values;
        private final String hello;
    
        public Immutable(List<Integer> values, String hello) {
            this.values = Collections.unmodifiableList(values);
           this.hello = hello;
        }
    
        public List<Integer> getValues() {
            return values;
        }
    
        public String getHello() {
            return hello;
        }
    }
    


  • 在immutable的访问器。

  • In the immutable's accessor (if applicable).

    public class Immutable {
    
        private final List<Integer> values;
        private final String hello;
    
        public Immutable(List<Integer> values, String hello) {
            this.values = values;
            this.hello = hello;
        }
    
        public List<Integer> getValues() {
            return Collections.unmodifiableList(values);
        }
    
        public String getHello() {
            return hello;
        }
    }
    


  • 有没有其他选项,哪一个是正确的?

    Are there any other options and which one is proper?

    推荐答案

    我想说如果你创建Immutable集合,保护自己免受修改作为参数的构造函数列表的修改,你应该保护性地复制它。

    I'd say if you create Immutable collection then you need to protect yourself from somebody modifying list that was given as an argument to constructor, you should protectively copy it.

    public Immutable(List<Integer> values, String hello) {
        this.values = Collections.unmodifiableList(new ArrayList<Integer>(values));
        this.hello = hello;
    }
    

    我个人很久以前已切换到 Guava 集合,因为您可以找到不可变集合的接口。您的构造函数将如下所示:

    I personally have long ago switched to Guava collections as there you can find interfaces for immutable collections. Your constructor would look like that:

    public Immutable(ImmutableList<Integer> values, String hello) {
        this.values = values;
        this.hello = hello;
    }
    

    您确定收到的参数不会被任何人修改。

    You'd be sure that argument you receive will not be modified by anyone.

    在不可变类中保持对不可变列表的引用通常是个好主意,因为它保证你不会错误地修改它。

    It is usually a good idea to keep reference to immutable list within your immutable class as it then guarantees that you don't modify it by mistake.

    在我看来case(1)只有当工厂方法是唯一的方法来创建不可变的集合,即使然后它可能会破坏,当有人重构这些类。除非有其他限制,否则最好创建自给自足的类。

    Case (1) in my opinion makes sense only when factory method is the only way to create the immutable collection and even then it may break when someone refactors these classes. Unless there are other limitations it is always best to create self-sufficient classes.

    这篇关于不可变对象和不可修改的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆