如何創(chuàng)造一個(gè)不可變類

不可變類就是創(chuàng)造以后就不能有任何改變的類,比如String
既然是不可變的,那么天然是線程安全的,而且可以緩存

1.class必須被聲明為final

禁止繼承,如果有子類,子類就可以覆蓋掉field,重寫method,變成一個(gè)隨便怎么樣的類,然后還屬于父類這個(gè)類型,那么父類就可以不是不可變類了

2.field都是private final,不能提供它們的set方法

3.構(gòu)造器初始化初始化field的時(shí),深拷貝

不能初始化的環(huán)境中還有這個(gè)對(duì)象的引用,不然就可以通過這個(gè)引用在外面改變這個(gè)對(duì)象的值

4.get方法中,不給field的引用,只深拷貝一份返回

原因和3一樣

@Slf4j
@ThreadSafe
public class ImmutableExample2 {

    private static  Map<Integer, Integer> map;

    static {
        Map<Integer, Integer>  m = Maps.newHashMap();
        m.put(1, 2);
        m.put(3, 4);
        m.put(5, 6);
        map = Collections.unmodifiableMap( m);
    }

    public static void main(String[] args) {

        map.put(1, 3); //這行報(bào)錯(cuò)
        log.info("{}", map.get(1));
    }

}

內(nèi)部實(shí)現(xiàn)是 重寫了put等修改的方法

        public V put(K key, V value) {
            throw new UnsupportedOperationException();
        }
        public V remove(Object key) {
            throw new UnsupportedOperationException();
        }
        public void putAll(Map<? extends K, ? extends V> m) {
            throw new UnsupportedOperationException();
        }
        public void clear() {
            throw new UnsupportedOperationException();
        }
@ThreadSafe
public class ImmutableExample3 {

    private final static ImmutableList<Integer> list = ImmutableList.of(1, 2, 3);

    private final static ImmutableSet set = ImmutableSet.copyOf(list);

    private final static ImmutableMap<Integer, Integer> map = ImmutableMap.of(1, 2, 3, 4);

    private final static ImmutableMap<Integer, Integer> map2 = ImmutableMap.<Integer, Integer>builder()
            .put(1, 2).put(3, 4).put(5, 6).build();


    public static void main(String[] args) {
        System.out.println(map2.get(3));
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容