所谓的单例模式,就是 采取一定的方法保证整个软件的系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法 (静态方法)
例如: Hibernate 中 SessionFactory,它充当数据存储源的代理,并负责创建 Session 对象。SessionFactory 并不是轻量级的,一般情况下,一个项目中只需要一个 SessionFactory,这就是单例模式
单例模式有八种
步骤:
//饿汉式(静态变量)
class Singleton {
//1. 构造器私有化, 防止外部new创建实例
private Singleton() {
}
//2.本类内部创建对象实例
private final static Singleton instance = new Singleton();
//3. 提供一个公有的静态方法,返回实例对象,主要是提供给外部,让外界引用
public static Singleton getInstance() {
return instance;
}
}
public class SingletonTest01 {
public static void main(String[] args) {
//测试
Singleton instance = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance == instance2); // true
System.out.println("instance.hashCode=" + instance.hashCode());
System.out.println("instance2.hashCode=" + instance2.hashCode());
}
}
因此:这种单例模式可用,但是可能会造成内存的浪费。除非开发人员能肯定,此类一定会被用到,那么内存就不会被浪费
//饿汉式(静态变量)
class Singleton {
//1. 构造器私有化
private Singleton() {
}
//2.本类内部创建对象实例
private static Singleton instance;
static { // 在静态代码块中,创建单例对象
instance = new Singleton();
}
//3. 提供一个公有的静态方法,返回实例对象
public static Singleton getInstance() {
return instance;
}
}
class Singleton {
private static Singleton instance;
// 构造函数私有化
private Singleton() {}
// 提供一个静态的公有方法,当使用到该方法时,才去创建instance,即懒汉式
public static Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class SingletonTest03 {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
Singleton instance2 = Singleton.getInstance();
System.out.println(instance == instance2); // true
System.out.println("instance.hashCode=" + instance.hashCode());
System.out.println("instance2.hashCode=" + instance2.hashCode());
}
}
// 懒汉式(线程安全,同步方法)
class Singleton {
private static Singleton instance;
// 构造方法私有
private Singleton() {}
//提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题
//即懒汉式
public static synchronized Singleton getInstance() {
if(instance == null) {
instance = new Singleton();
}
return instance;
}
}
注:加入关键字保证线程安全问题 synchronized
不推荐使用
class Singleton {
private static volatile Singleton instance;
// 构造方法私有化
private Singleton(){}
// 提供可以被外部引用的静态方法,使用双重检查机制 double check
public static synchronized Singleton getInstance(){
if (instance == null){
synchronized(Singleton.class){
if(instance == null){
instance = new Singleton();
}
}
}
return instance;
}
}
// 静态内部类完成, 推荐使用
class Singleton {
private static volatile Singleton instance;
//构造器私有化
private Singleton() {}
//写一个静态内部类,该类中有一个静态属性Singleton
private static class SingletonInstance {
private static final Singleton INSTANCE = new Singleton();
}
//提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE
public static synchronized Singleton getInstance() {
return SingletonInstance.INSTANCE;
}
}
//使用枚举,可以实现单例, 推荐
enum Singleton {
INSTANCE; //属性
public void sayOK() {
System.out.println("ok~");
}
}
public class SingletonTest08 {
public static void main(String[] args) {
Singleton instance = Singleton.INSTANCE;
Singleton instance2 = Singleton.INSTANCE;
System.out.println(instance == instance2);
System.out.println(instance.hashCode());
System.out.println(instance2.hashCode());
instance.sayOK();
}
}
JDK中,java.lang.Runtime 就是经典的单例模式(饿汉式)
原文:https://www.cnblogs.com/shanggushenlong/p/13801414.html