设计模式之单例模式

单例设计模式:singleton

  • 所谓单例就是入口处(构造方法)限制对象的实例化操作
  • 核心就是将类的构造方法私有化,之后在类的内部产生实例化对象,并通过类的静态方法返回实例化对象的引用。

简单实现:

 

public class Singleton{

        //饿汉式
        //私有的静态的成员变量
	private static final Singleton instance= new Singleton();

	private Singleton(){}  //构造方法私有化

	public static Singleton getInstance() {
		return instance;
	}
}

 

 

 

构造方法私有化-->在类的内部产生实例化对象

 

即使产生多个singleton对象,所有对象都只有一个instance引用

 
饿汉式在类实例化时就生成实例化对象,可以修改为懒汉式,懒汉式的特点在于实例延迟加载

 

public class Singleton {

	private static Singleton instance = null;

	private Singleton() {}

	public static Singleton getInstance() {
		if (instance == null) {
			instance = new Singleton();
		}
		return instance;
	}

}

 
 懒汉式在多线程下,会产生安全隐患(在需要它创建时还没有赋值,正准备赋值时,CPU切换到第二个线程,第二个线程看到是null,又创建和赋值,内存出现两个对象,解决办法,加上互斥)

 

public class Singleton {

	private static Singleton instance = null;

	private Singleton() {}

	public static synchronized  Singleton getInstance() {
		if (instance == null) {
			instance = new Singleton();
		}
		return instance;
	}

}

 

但是因为每次调用前都会对同步锁进行判断,多次调用多次判断十分低效

可以改为双检索

public class Singleton {

	private static Singleton instance = null;

	private Singleton() {}

	public static  Singleton getInstance() {
		synchronized (Singleton.class) {
			if (instance == null) {
				if (instance == null) {
					instance = new Singleton();
				}
			}
		}
		return instance;
	}

}

 

 示例:单例读取配置文件

import java.io.*;
import java.util.*

public class SystemConfigUtil {
	private static SystemConfigUtil systemConfigUtil = null;
	private static Map<String, String> proMap = new HashMap<String, String>();

	@SuppressWarnings("unchecked")
	private SystemConfigUtil() throws Exception {
		File file = new File("d:/demo.properties");
		InputStream is = null;
		Properties pro = new Properties();

		is = new FileInputStream(file);
		pro.load(is);
		Enumeration e = pro.propertyNames();
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			String value = (String) pro.get(key);
			proMap.put(key, value);

		}
		is.close();
	}

	public static SystemConfigUtil getInstance() throws Exception {
		synchronized (SystemConfigUtil.class) {
			if (systemConfigUtil == null) {
				systemConfigUtil = new SystemConfigUtil();
			}
		}
	
		return systemConfigUtil;
	}

	public static Map<String, String> getProMap() {
		return proMap;
	}

	@SuppressWarnings( { "unchecked" })
	public static void main(String[] args) throws Exception {
		SystemConfigUtil d1 = SystemConfigUtil.getInstance();
		SystemConfigUtil d2 = SystemConfigUtil.getInstance();
		System.out.println(d1 == d2);  //true
		Map m1 = SystemConfigUtil.getProMap();
		Map m2 = SystemConfigUtil.getProMap();
		System.out.println(m1 == m2); //true
		System.out.println(m1.get("demo"));
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值