java 单例模式实现代码


1、使用静态内部类实现

使用静态内部类实现单例模式,线程安全

class SingletonStaticInner {
        private SingletonStaticInner() {}
        private static class SingletonInner {
		private static SingletonStaticInner singletonStaticInner = new SingletonStaticInner();
	}

	public static SingletonStaticInner getInstance() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return SingletonInner.singletonStaticInner;
	}
}

2、使用枚举实现

class Resource{}

public enum SomeThing {
    INSTANCE;
    private Resource instance;
    SomeThing() {
        instance = new Resource();
    }
    public Resource getInstance() {
        return instance;
    }
}

3、序列化与反序列化

单例模式被破坏解决方式:
如下代码,序列化和反序列化的情况下会出现生成多个对象的情况,违背了单例模式。

public class Test {

	public static void main(String[] args) {
		try {
			Ssi serialize = Ssi.getInstance();
			System.out.println(serialize.hashCode());
			//序列化
			FileOutputStream fo = new FileOutputStream("tem");
			ObjectOutputStream oo = new ObjectOutputStream(fo);
			oo.writeObject(serialize);
			oo.close();
			fo.close();
			//反序列化
			FileInputStream fi = new FileInputStream("tem");
			ObjectInputStream oi = new ObjectInputStream(fi);
			Ssi serialize2 = (Ssi) oi.readObject();
			oi.close();
			fi.close();
			System.out.println(serialize2.hashCode());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

//使用匿名内部类实现单例模式,在遇见序列化和反序列化的场景,得到的不是同一个实例
//解决这个问题是在序列化的时候使用readResolve方法,即去掉注释的部分
class Ssi implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private static class InnerClass {
		private static Ssi ssi = new Ssi();
	}
	
	public static Ssi getInstance() {
		return InnerClass.ssi;
	}
	
//	protected Object readResolve() {
//		System.out.println("调用了readResolve方法");
//		return InnerClass.singletonStaticInnerSerialize;
//	}
}

解决办法就是在反序列化中使用readResolve()方法(将上面的注释代码去掉就可以了)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM