介绍:
采取一定的方法保证在整个的软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法(静态方法)。
创建的八种模式:
- 饿汉式(静态常量)
- 饿汉式(静态代码块)
- 懒汉式(线程不安全)
- 懒汉式(线程安全,同步方法)
- 懒汉式(线程安全,同步代码块)
- 双重检查
- 静态内部类
- 枚举
八种模式的解释:
-
饿汉式(静态常量)
创建的方式
- 构造器私有化(防止new)
- 类的内部创建对象
- 向外暴露一个静态的公共方法
/**
代码实现
*/
public class Singleton1{
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());//.hsahCode相当于查看地址
}
}
class Singleton{
private Singleton(){}//第一步:构造器私有化,防止外部通过new的方式创建对象实例
private final static Singleton instance = new Singleton();//第二步:在类的内部创建对象
public static Singleton getInstance(){//第三步:提供一个公有的静态方法,返回实例对象
return instance;
}
}
解析:
通过这种方法创建的饿汉式
优点:这种写法比较简单,在类装载的时候即可完成实例化。避免线程同步问题。
缺点:在类装载的时候即可完成实例化,但没有达到Lazy Loading的效果。假如这个实例为从被使用过,则会造成内存的浪费。
这种方式基于classloder机制避免了多线程的同步问题,不过instance在类装载时即可实例化,在单例模式大多数都是调用getInstance方法,但是导致类装载的原因有很多种,因此不能确定有其他方式(或者其他的静态方法)导致类装载,这时候初始化instance没有达到 lazy loading 的效果。
总结:这种单例模式可用,但可能会造成内存浪费
-
饿汉式(静态代码块)
这方式与上面的方式非常类似,唯一的不同时将实例化的过程放在了静态代码块中(补充:静态代码块在类加载时会执行静态代码块的代码)
/**
代码实现
*/
public class Singleton1{
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());//.hsahCode相当于查看地址
}
}
class Singleton{
private Singleton(){}//第一步:构造器私有化,防止外部通过new的方式创建对象实例
private static Singleton instance;//第二步:创建私有变量
static{
instance = new Singleton();//第三步:在静态代码块实例化
}
public static Singleton getInstance(){//第四步:提供一个公有的静态方法,返回实例对象
return instance;
}
}
总结:这种单例模式可用,但可能会造成内存浪费
-
懒汉式(线程不安全)
该方式可以起到Lazy Loading的效果,但只能在单线程下使用,在多线程的环境下使用时,会产生多个实例, 这种单例模式在日常开发中不推荐使用
public class SingletonTest{
public static void main(String[] args){
Singleton intstance = Singleton.getInstance();
Singleton intstance2 = Singleton.getInstance();
System.out.println(instance==instance2);
System.out.println("instance.hashCode="+instance.hashCode());
System.out.println("instance2.hashCode="+instance2.hashCode());
}
}
class Singleton{
private static Singleton instance;
private Singleton(){}
public static Singleton getInstance(){//提供一个静态公有方法,当使用 到该方法时,才去创建instance
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
-
懒汉式(线程安全,同步方法)
特点:线程安全,效率低。不推荐使用
public class SingletonTest{
public static void main(String[] args){
Singleton intstance = Singleton.getInstance();
Singleton intstance2 = Singleton.getInstance();
System.out.println(instance==instance2);
System.out.println("instance.hashCode="+instance.hashCode());
System.out.println("instance2.hashCode="+instance2.hashCode());
}
}
class Singleton{
private static Singleton instance;
private Singleton(){}
//提供一个静态公有方法,使用synchronized来修饰方法,解决线程安全问题
public static synchronized Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
-
懒汉式(线程安全,同步代码块)
特点:线程安全,效率低。不推荐使用
public class SingletonTest{
public static void main(String[] args){
Singleton intstance = Singleton.getInstance();
Singleton intstance2 = Singleton.getInstance();
System.out.println(instance==instance2);
System.out.println("instance.hashCode="+instance.hashCode());
System.out.println("instance2.hashCode="+instance2.hashCode());
}
}
class Singleton{
private static Singleton instance;
private Singleton(){}
//提供一个静态公有方法,使用synchronized来修饰方法内的代码,解决线程安全问题
public static Singleton getInstance(){
if(instance == null){
synchronized (String.class){
singleton = new Singleton();
}
}
return instance;
}
}
-
双重检查(Double-Check)
这是方式是在多线程开发中常使用到的,具有线程安全、延迟加载、效率较高的优点。在实际开发中推荐使用。
public class SingletonTest{
public static void main(String[] args){
Singleton intstance = Singleton.getInstance();
Singleton intstance2 = Singleton.getInstance();
System.out.println(instance==instance2);
System.out.println("instance.hashCode="+instance.hashCode());
System.out.println("instance2.hashCode="+instance2.hashCode());
}
}
class Singleton{
private static Singleton instance;
private Singleton(){}
//提供一个静态公有方法,使用synchronized来修饰方法并加入双重检查代码,解决线程安全问题和懒加载问题
public static synchronized Singleton getInstance(){
if(instance == null){
synchronized (String.class){
if(instance == null){//这里进行了两次if(instance == null)检查来保证线程安全问题
singleton = new Singleton();
}
}
}
return instance;//这样实例化代码只用执行一次,后面再次访问时,判断if(instance == null),直接return实例化对象,同时也避免反复进行方法同步
}
}
-
静态内部类
效率高,利用了静态内部类特点实现延迟加载。推荐使用
public class SingletonTest{
public static void main(String[] args){
Singleton intstance = Singleton.getInstance();
Singleton intstance2 = Singleton.getInstance();
System.out.println(instance==instance2);
System.out.println("instance.hashCode="+instance.hashCode());
System.out.println("instance2.hashCode="+instance2.hashCode());
}
}
class Singleton{
private static Singleton instance;
private Singleton(){}
//提供一个静态私有的内部类,该类创建一个静态不可变的属性
private static class getInstance(){
private static final Singleton INSTANCE = new Singleton();
}
//对外提供一个公共静态用synchronized修饰的方法,来实现线程安全,返回SingletonInstance.INSTANCE实例对象。
public static synchronized Singleton getInstance(){
return SingletonInstance.INSTANCE;
}
}
-
枚举
这种方式是Effective Java作者Josh Blocj 提倡的方式。
借助了JDK1.5中添加的枚举来实现单例模式,不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。
public class SingletonTest{
public static void main(String[] args){
Singleton intstance = Singleton.getInstance();
Singleton intstance2 = Singleton.getInstance();
System.out.println(instance==instance2);
System.out.println("instance.hashCode="+instance.hashCode());
System.out.println("instance2.hashCode="+instance2.hashCode());
instance.sayOK();
}
}
enum Singleton{
INSRANCE;
public void sayOK(){
System.out.println("ok...");
}
}
}
注意事项与细节说明:
- 单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式可以提高系统性能;
- 单想实例化一个单例类的时候,必须要记住使用相应的获取对象的方法,而不是通过new的方式创建实例;
- 单例模式使用的场景:需要频繁的进行创建和销毁的对象、创建对象是耗时过多或耗费资源过多(即:重量级对象),但又经常用到的对象、工具类对象、频繁访问数据或文件的对象(比如数据源、session工厂等)。