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

单例模式

  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
今日推荐