싱글톤 모드(게으르고 배고픈 작성의 여러 방법)

싱글톤 패턴

  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;
    }
}

추천

출처blog.csdn.net/Da_Bao_zi/article/details/124370522