1-java单例模式

常见的单例模式由5种

1.饿汉模式

package day01.pattern;

import java.io.Serializable;

//  饿汉模式
class Singleton1 implements Serializable {
    //  给静态变量赋值,会放到静态代码块执行,静态代码块的线程安全由JVM保证
    private static final Singleton1 INSTANCE = new Singleton1();

    //  构造方法私有
    private Singleton1() {
        //  预防反射破坏单例
        if(INSTANCE!=null){

            throw new RuntimeException("单例对象不能重复创建");
        }
    }

    //  预防反序列化破坏单例
    public Object readResolve(){
        return INSTANCE;
    }

    public static Singleton1 getINSTANCE() {
        return INSTANCE;
    }
}

2.枚举饿汉模式

package day01.pattern;

//  枚举饿汉式
public enum Singleton2 {
    INSTANCE;
    private Singleton2(){
        System.out.println("privet Singleton2()");
    }

    @Override 
    public String toString() {
        return getClass().getName()+"@"+Integer.toHexString(hashCode());
    }

    public static Singleton2 getINSTANCE() {
        return INSTANCE;
    }
    public static void otherMethod(){
        System.out.println("otherMethod");
    }
}

3.懒汉模式

package day01.pattern;

import java.io.Serializable;

//  懒汉模式
class Singleton3 implements Serializable {

    private static  Singleton3 INSTANCE = null;

    //  构造方法私有
    private Singleton3() {
        System.out.println("private singleton3()");
    }
    //  给当前class对象上加上锁
    public static synchronized Singleton3 getINSTANCE() {
        if(INSTANCE == null) {
            INSTANCE = new Singleton3();
        }
        return INSTANCE;
    }

    public static void otherMethod(){
        System.out.println("otherMethod");
    }

}

4.懒汉模式 DCL (双检锁)

package day01.pattern;

import java.io.Serializable;

//  懒汉式单例 - DCL(双检锁)
class Singleton4 implements Serializable {

    //  volatile 可见性,有序性
    private  volatile static Singleton4 INSTANCE = null;

    //  构造方法私有
    private Singleton4() {
        System.out.println("private singleton4()");
    }
    //
    public static  Singleton4 getINSTANCE() {
        if(INSTANCE == null) {
            synchronized (Singleton4.class) {
                if(INSTANCE == null) {
                    INSTANCE = new Singleton4();
                }
            }
        }
        return INSTANCE;
    }

    public static void otherMethod(){
        System.out.println("otherMethod");
    }

}

5.内部类懒汉式

package day01.pattern;

import java.io.Serializable;

//  懒汉式单例 - 内部类
class Singleton5 implements Serializable {

    //  静态内部类
    private static class Holder{
        static Singleton5 INSTANCE = new Singleton5();
    }


    //  构造方法私有
    private Singleton5() {
        System.out.println("private singleton5()");
    }
    //
    public static Singleton5 getINSTANCE() {
       return Holder.INSTANCE;
    }

    public static void otherMethod(){
        System.out.println("otherMethod");
    }

}

JDK中有哪些地方体现了单例模式

Runtime.class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值