单例模式(懒汉式和饿汉式的几种写法)

本文详细介绍了Java中的单例模式,包括饿汉式和懒汉式的实现方式,如静态变量、静态代码块、线程安全与不安全的懒汉式、双检锁、内部类和枚举实现。单例模式的核心特点是构造器私有化、提供单例获取方法,并确保只有一个实例存在。同时,文章讨论了不同实现的线程安全性和性能影响。
摘要由CSDN通过智能技术生成

单例模式

  1. 属于建造者模式,就是创建对象的模式,单例类,创建实例对象,不需要new直接使用

  2. 角色分为:单例类 和 访问类

  3. 饿汉式 和 懒汉式

  4. //单例分为 饿汉式和懒汉式
    //饿汉式分为:
    //静态变量
    //静态代码块
    
    //懒汉式分为:
    //线程不安全
    //线程安全
    //双检锁
    //内部类
    //枚举
    
    //单例的特点
    //1.构造方法私有化
    //2.提供一个返回单例对象本身的公共方法
     //3.提供一个静态私有的变量
    
    
    public class SingletonsTest {
        public static void main(String[] args) {
            Singleton singleton=Singleton.getInstance();
            Singleton singleton2=Singleton.getInstance();
        }
    
    
    }
    // 饿汉式-静态变量
    class Singleton {
        //3.提供一个静态私有的变量
        private static Singleton instance = new Singleton();
    
        //1.构造器私有化
        private Singleton() {}
    
        //2.提供一个返回单例对象本身的公共方法
        public static Singleton getInstance() {
            return instance;
        }
    }
    
    
public class SingletonTest2 {
    public static void main(String[] args) {
        Singleton1 singleton=Singleton1.getInstance();
        Singleton1 singleton1=Singleton1.getInstance();

        System.out.println(singleton==singleton1);
    }

}


class Singleton1 {
    private static Singleton1 instance;
    //静态代码块版本的 饿汉式
    static {
        instance = new Singleton1();
    }

    private Singleton1() {
    }

    public static Singleton1 getInstance() {
        return instance;
    }
}
懒汉式
import java.util.Properties;

public class SingletonTest3 {
    public static void main(String[] args) {
//        Singleton3 singleton3 = Singleton3.getInstance();
//        Singleton3 singleton4 = Singleton3.getInstance();
//
//        System.out.println(singleton3 == singleton4);



    }
}

//枚举:构造器私有
enum Color {
    Red(1), Blue(1), Green(1);

    private Color(int n){

    }
    public void show() {

    }
}
//懒汉式 枚举 可以防止反射  每一个枚举类都是Enum的子类  最安全的最好的懒汉单例
enum SingletonEnum{
    Instance;
    private SingletonEnum(){
        System.out.println("枚举类被实例化");
    }
    final static Properties prop=new Properties();

    public static Properties getProperties(){
        prop.setProperty("driver","com.mysql.jdbc.Driver");
        prop.setProperty("user","root");
        return prop;
    }
}

//懒汉式 内部类
class Singleton3_4 {
    private static Singleton3_4 instance;

    private Singleton3_4() {
    }

    private static class SingletonHolder {
        private static Singleton3_4 INSTANCE = new Singleton3_4();
    }

    public static Singleton3_4 getINSTANCE() {
        if (instance == null) {
            instance = SingletonHolder.INSTANCE;
        }
        return instance;
    }

}


//懒汉式 线程安全(优化法)(双重验证)
class Singleton3_3 {
    private static Singleton3_3 instance;

    private Singleton3_3() {
    }

    public static Singleton3_3 getInstance() {
        if (instance == null) {//为空  加了判断就不用再创建对象了
            //只锁关键部分
            synchronized (Singleton3_3.class) {
                if (instance == null) {
                    instance = new Singleton3_3();
                }

            }
        }
        return instance;
    }

}


//懒汉式 线程安全
class Singleton3_2 {
    private static Singleton3_2 instance;

    private Singleton3_2() {
    }

    public synchronized static Singleton3_2 getInstance() {
        //synchronized  static 锁的是字节码文件 .class 字节码文件是唯一的
        //synchronized  锁的是当前对象
        if (instance == null) {
            instance = new Singleton3_2();
        }
        return instance;
    }
}

//懒汉式 线程不安全
class Singleton3 {
    private static Singleton3 instance;

    private Singleton3() {
    }

    public static Singleton3 getInstance() {
        if (instance == null) {
            instance = new Singleton3();
        }
        return instance;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值