单例模式之饿汉模式实现

单例模式之饿汉模式实现
不要问为什么,问就是爱过,上代码:

package singleton;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 
 * @ClassName:  HungrySingleton   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: 长子科技 
 * @date:   2021年7月7日 下午9:07:35   
 *     
 * @Copyright: 2021 www.tydic.com Inc. All rights reserved. 
 * 注意:本内容仅限于长子科技股份有限公司内部传阅,禁止外泄以及用于其他的商业目
 */
public class HungrySingleton implements Cloneable,Serializable{
	/**   
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)   
	 */  
	private static final long serialVersionUID = 1L;
	private volatile static HungrySingleton instance=new HungrySingleton();
	
	//避免反射产生新的单例
	private HungrySingleton(){
		if(instance!=null){
			throw new RuntimeException("单例已经实例化了,不能再实例化对象了");
		}
	}
	public static HungrySingleton getInstance() {
		return instance;
	}
	
	//避免克隆产生多个实例。
	@Override
	public HungrySingleton clone(){
		return instance;
	}
	//避免反序列化导致产生多个实例
	private Object readResolve(){
        return instance;
    }
	
	public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException, ClassNotFoundException, CloneNotSupportedException {
		
		HungrySingleton instance1=HungrySingleton.getInstance();
		System.out.println("instance1:"+instance1.hashCode());
		
		//通过反序列话获取单例
		ByteArrayOutputStream bos=new ByteArrayOutputStream();
		ObjectOutputStream oos=new ObjectOutputStream(bos);
		oos.writeObject(instance1);
		ByteArrayInputStream bis=new ByteArrayInputStream(bos.toByteArray());
		ObjectInputStream ois=new ObjectInputStream(bis);
		HungrySingleton instance3=(HungrySingleton)ois.readObject();
		System.out.println("instance3:"+instance3.hashCode());
		//通过克隆获取对象
		HungrySingleton instance4=(HungrySingleton) instance1.clone();
		System.out.println("instance4:"+instance4.hashCode());
		
		//反射方式获取对象
		Constructor<HungrySingleton> declaredconstructor=HungrySingleton.class.getDeclaredConstructor();
		declaredconstructor.setAccessible(true);
		HungrySingleton instance2=declaredconstructor.newInstance();
		System.out.println("instance2:"+instance2.hashCode());
	}
}

结果:
instance1:366712642
instance3:366712642
instance4:366712642
Exception in thread “main” java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at singleton.HungrySingleton.main(HungrySingleton.java:69)
Caused by: java.lang.RuntimeException: 单例已经实例化了,不能再实例化对象了
at singleton.HungrySingleton.(HungrySingleton.java:32)
… 5 more

经过上一节(单例模式之懒汉模式实现)的演示,本章直接上最终代码,给大家,看一个安全的饿汉单例模式的实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一截木化石

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值