Java设计模式-单例模式

Java设计模式-单例模式

注意点
1.单例模式保证了 系统中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式提高系统性能
2.当想实例化一个单例类的时候,必须要记住使用相应的获取对象的方法,而不是使用new
3.场景:需要频繁创建销毁的对象,创建对象时耗过多或耗费资源过多(重量级队形),但又经常用到的对象、工具类、频繁访问数据库或文件的对象 比如数据源、session工厂等。

创建方式

  1. 饿汉式1
package cn.com.singleton.type1;

public class SingletonTest01 {
    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1.hashCode() == instance2.hashCode());
    }
}

//饿汉式(静态变量)   类加载时候就会被创建,没实现懒加载,造成内存浪费
class Singleton {

    private Singleton() {
    }

    private final static Singleton instance = new Singleton();

    public static Singleton getInstance() {
        return instance;
    }
}
  1. 饿汉式2
package cn.com.singleton.type2;

public class SingletonTest02 {
    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1.hashCode() == instance2.hashCode());

    }
}

//饿汉式(静态代码块)   类加载时候就会被创建,没实现懒加载,也会造成内存浪费
class Singleton {

    private Singleton() {

    }

    private static Singleton instance;

    static { //静态代码块中创建单例
        instance = new Singleton();
    }

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

3.懒汉式-线程不安全

package cn.com.singleton.type3;

public class SingletonTest03 {
    public static void main(String[] args) {
        System.out.println("懒汉式,线程不安全的");
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1.hashCode() == instance2.hashCode());

    }
}

//懒汉式  当需要时候才创建 线程不安全  只能用于单线程,实际开发中不能使用
/*  如果在多线程下,一个线程进入了if ,这时候未执行完,另外一个线程会同时进入这个判断语句内
这时会产生多个实例*/
class Singleton {
    private static Singleton instance;

    private Singleton() {

    }

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

4.懒汉式-线程安全

package cn.com.singleton.type4;

public class SingletonTest04 {
    public static void main(String[] args) {
        System.out.println("懒汉式2,线程安全");
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1.hashCode() == instance2.hashCode());

    }
}

//懒汉式-线程安全  加锁synchronized,当其他线程执行方法,其它线程等待

/**
 1. 效率太低,每个线程在想获得类的实例的时候,执行getInstance 方法都要进行同步。二其实这个方法
 2. 只执行一次实例化代码就够了,后面的想获得该类的实例,直接return 即可
 */
class Singleton {
    private static Singleton instance;

    private Singleton() {

    }

    //加入同步处理方法 synchronized
    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    //方法2 导致线程不安全
    /**
     *  下面这种方式本意是想对上诉1方法改进,因为同步方法效率太低,改为同步产生实例的代码块
     *  但是这种同步并不能起到线程同步的作用。跟type3中情况相似,如果在多线程下,一个线程进入了if,
     * 这时候未执行完,另外一个线程会同时进入这个判断语句内这时会产生多个实例
     */
    public static Singleton getInstance2() {

        if (instance == null) {
            synchronized (Singleton.class) {
                instance = new Singleton();
            }
        }
        return instance;
    }
}
  1. 双重检查 推荐使用
package cn.com.singleton.type5_recommend;

public class SingletonTest05 {
    public static void main(String[] args) {
        System.out.println("双重检查,线程安全");
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1 == instance2);
        System.out.println(instance1.hashCode() == instance2.hashCode());

    }
}

//双重检查  推荐使用
class Singleton {
    private static volatile Singleton instance;

    private Singleton() {

    }

    // 双重检查,解决线程安全,同事解决了懒加载问题
    //  volatile 对象,表示立刻写入内存,对于其他线程立即可见
    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null)
                    instance = new Singleton();
            }
        }
        return instance;
    }

}
  1. 静态内部类实现 推荐使用
package cn.com.singleton.type6_recommend;

public class SingletonTest06 {
    public static void main(String[] args) {
        System.out.println("使用静态内部类实现单例模式");
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1 == instance2);
        System.out.println(instance1.hashCode() == instance2.hashCode());

    }
}

//静态内部类实现 推荐使用
/**
 * 1)这种方式采用了的装载机制来保证初始化实例时候只有一个线程
 * 2)静态内部类方式Singleton类被装载是并不会立即实例化,而是需要实例化时候,调用getInstance 方法
 * ,才会装载SingletonInstance类,从而完成Singleton的实例化
 * 3)类的静态属性石灰在第一次加载类的时候初始化,JVM帮助我们保证了线程的安全性,在类初始化的时其他
 * 线程是无法进入的。
 * 保证了线程安全,利用静态内部类实现了延迟加载,效率高
 */
class Singleton {
    private static volatile Singleton instance;

    private Singleton() {

    }
    //静态类不类,该类里有个静态属性Singleton
    private static class SingletonInstance{
        private static final Singleton INSTANCE=new Singleton();
    }

    public static Singleton getInstance() {
        return SingletonInstance.INSTANCE;
    }

}

7.使用枚举实现单例 推荐使用

package cn.com.singleton.type7_recommend;

public class SingletonTest07 {
    public static void main(String[] args) {
        Singleton instance1 = Singleton.INSTANCE;
        Singleton instance2 = Singleton.INSTANCE;
        System.out.println(instance1 == instance2);
    }

}

//使用枚举实现单例 推荐使用
/**
 *  不仅能避免多线程同步问题,而且还能防止反序列化重新创建新对象
 */
enum Singleton {
    INSTANCE
}
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值