发布时间:2022-11-01 文章分类:编程知识 投稿人:李佳 字号: 默认 | | 超大 打印

单例模式

饿汉式(静态常量)

/**
 * 饿汉式(静态常量)
 */
public class Singleton01 {
    public static void main(String[] args) {
        //测试
        Singleton instance = Singleton.getInstance();
    }
}
class Singleton{
    //1 构造器私有化
    private Singleton(){
    }
    //2 本类内部创建对象实例
    private final static Singleton instance = new Singleton();
    //3 提供一个公有的静态方法,返回实例对象
    public static Singleton getInstance(){
        return instance;
    }
}

饿汉式(静态代码块)

/**
 * 饿汉式(静态代码块)
 */
public class Singleton02 {
    public static void main(String[] args) {
        //测试
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance.hashCode() == instance1.hashCode());
    }
}
class Singleton{
    //1 构造器私有化
    private Singleton(){
    }
    //2 本类内部创建对象实例
    private  static Singleton instance;
    //在静态代码块种创建对象实例
    static {
        instance = new Singleton();
    }
    //3 提供一个公有的静态方法,返回实例对象
    public static Singleton getInstance(){
        return instance;
    }
}

懒汉式(线程不安全)

/**
 * 懒汉式(线程不安全)
 */
public class Singleton03 {
    public static void main(String[] args) {
        //测试
        Singleton instance = Singleton.getInstance();
        Singleton instance1 = Singleton.getInstance();
        System.out.println(instance.hashCode() == instance1.hashCode());
    }
}
class Singleton{
    private static Singleton instance;
    private Singleton(){
    }
    //提供静态的公用方法,当使用该方法时,才创建实例
    public static Singleton getInstance(){
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}

懒汉式(线程安全)

/**
 * 懒汉式(线程安全,同步方法)
 */
class Singleton{
    private static Singleton instance;
    private Singleton(){
    }
    //使用synchronized解决线程安全问题
    public static synchronized Singleton getInstance(){
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}

双重检查

/**
 * 双重检查
 */
class Singleton{
    private static volatile Singleton instance;
    private Singleton(){
    }
    //加入双重检查代码,解决线程安全问题,通知解决懒加载问题
    public static  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();
    }
    public static  Singleton getInstance(){
        return SingletonInstance.instance;
    }
}

枚举

public class Singleton07 {
    public static void main(String[] args) {
        //测试
        Singleton instance = Singleton.INSTANCE;
        Singleton instance1 = Singleton.INSTANCE;
        System.out.println(instance.hashCode() == instance1.hashCode());
    }
}
/**
 * 枚举
 */
enum  Singleton{
    INSTANCE;
}