单例设计模式

单例设计模式:singleton

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

Java代码 收藏代码
public class Singleton{

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

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

public static Singleton getInstance() {   
    return instance;   
}   

}

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

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

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

Java代码 收藏代码
public class Singleton {

private static Singleton instance = null;   

private Singleton() {}   

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

}

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

Java代码 收藏代码
public class Singleton {

private static Singleton instance = null;   

private Singleton() {}   

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

}

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

可以改为双检索

Java代码 收藏代码
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;   
}   

}

示例:单例读取配置文件

Java代码 收藏代码
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"));   
}   

}

你可能感兴趣的:(单例设计模式)