首页 新闻 会员 周边

静态内部类实现的单例序列化后怎么保证

0
悬赏园豆:10 [待解决问题]

我重写了readobject和writeobject,但是还是不能保证单例。

package com.citi.designpattern.singleton;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SingletonTest1 implements Serializable{
    

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private SingletonTest1(){
        
    }
    
    private static SingletonTest1 getInstance(){
        return Nested.instance;
    }
    
    static class Nested{
        private static SingletonTest1 instance = new SingletonTest1();
    }
    
    
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException{
        //1. for not Serizable
        /*SingletonTest1 instance1 = SingletonTest1.getInstance();
        SingletonTest1 instance2 = SingletonTest1.getInstance();
        
        System.out.println(instance1 == instance2);*/
        
        //2. for Serizable 
        /*SingletonTest1 instance = SingletonTest1.getInstance();
                
        File file = new File("ser.out");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        oos.writeObject(instance);
        oos.close();
        
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        SingletonTest1 oIstance = (SingletonTest1)ois.readObject();
        ois.close();
        System.out.println(oIstance == instance);    */
        
        //3. overwrite writeObject and readObject
        //SingletonTest1 instance = SingletonTest1.getInstance();
        
        File file = new File("ser.out");
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
        new SingletonTest1().writeObject(oos);
        oos.close();
        
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        System.out.println(new SingletonTest1().readObject(ois) == SingletonTest1.getInstance());
        ois.close();
    }
    
    private void writeObject(ObjectOutputStream oos) throws IOException { 
        //oos.defaultWriteObject();
        oos.writeObject(SingletonTest1.getInstance());        
    }
    
    private  SingletonTest1 readObject(ObjectInputStream ois) throws IOException,  
    ClassNotFoundException { 
        //ois.defaultReadObject();
        return (SingletonTest1)ois.readObject();
    } 
    
    

}

结果是false。

我重写的这俩方法应该有问题,求助。谢谢。

Ruth/Christy的主页 Ruth/Christy | 菜鸟二级 | 园豆:222
提问于:2014-05-22 14:35
< >
分享
所有回答(2)
0

当然是false了。对象写的读只是copy了一个内容是一样的对象。它们的内存地址还是不同的。如果你想让一个对象内容相同,就相等的话,要覆盖hashcode ,equal方法什么的,具体的我不记得了。

angelshelter | 园豆:9887 (大侠五级) | 2014-05-22 16:56

个人觉得这不需要重写hashcode和equals,但是前面的我同意。找了一代替方案,重写readReslove

支持(0) 反对(0) Ruth/Christy | 园豆:222 (菜鸟二级) | 2014-05-27 11:21
0

百度 == 和equal 的区别。

Rookier | 园豆:652 (小虾三级) | 2014-05-23 14:24

这跟==和equals有什么关系。。个么你觉得我得重写下equals和hashcode?

支持(0) 反对(0) Ruth/Christy | 园豆:222 (菜鸟二级) | 2014-05-27 11:19

@Ruth/Christy: ==操作符会去调用equals方法,默认的equals方法会比较内存地址(或者说引用),如果你想改变规则为内容相同就是相等,那就要覆盖原来的方法, 看你的实际需求了。

支持(0) 反对(0) angelshelter | 园豆:9887 (大侠五级) | 2014-05-27 11:25

@angelshelter: 我的需求是实现序列话,然后单例。==来自两方面equals和hashcode方法,介个我知道。

支持(0) 反对(0) Ruth/Christy | 园豆:222 (菜鸟二级) | 2014-05-27 13:47

@Ruth/Christy: 你不会是想序列化出来对象,都是同一个对象吧?

支持(0) 反对(0) angelshelter | 园豆:9887 (大侠五级) | 2014-05-27 17:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册