(二)JAVA设计模式——单例模式的七种实现方式

饿汉式(静态常量)

package com.yundi.atp.dp.singleton;

/**
 * @Author: yanp
 * @Description: 饿汉式(静态常量)
 * 线程安全,对象实例化的时候构建,可能造成内存浪费,没有懒加载的效果
 * @Date: 2022/3/12 12:06
 * @Version: 1.0.0
 */
public class SingletonOne {
    /**
     * 1.私有化构造器
     */
    private SingletonOne() {

    }

    /**
     * 2.创建实例对象
     */
    private final static SingletonOne INSTANCE = new SingletonOne();

    /**
     * 3.获取实例方法
     *
     * @return
     */
    public static SingletonOne getInstance() {
        return INSTANCE;
    }

    public static void main(String[] args) {
        SingletonOne instance = SingletonOne.getInstance();
        System.out.println(instance);
    }
}

饿汉式(静态代码块)

package com.yundi.atp.dp.singleton;

/**
 * @Author: yanp
 * @Description: 饿汉式(静态代码块)
 * @Date: 2022/3/12 12:06
 * @Version: 1.0.0
 */
public class SingletonTwo {
    /**
     * 1.私有化构造器
     */
    private SingletonTwo() {

    }

    /**
     * 2.声明实例对象
     */
    private static SingletonTwo INSTANCE;

    /**
     * 3. 创建实例对象
     */
    static {
        INSTANCE = new SingletonTwo();
    }

    /**
     * 4.获取实例方法
     *
     * @return
     */
    public static SingletonTwo getInstance() {
        return INSTANCE;
    }

    public static void main(String[] args) {
        SingletonTwo instance = SingletonTwo.getInstance();
        System.out.println(instance);
    }
}

懒汉式(线程不安全)

package com.yundi.atp.dp.singleton;

/**
 * @Author: yanp
 * @Description: 懒汉式(线程不安全)
 * @Date: 2022/3/12 12:06
 * @Version: 1.0.0
 */
public class SingletonThree {
    /**
     * 1.私有化构造器
     */
    private SingletonThree() {

    }

    /**
     * 2.声明实例对象
     */
    private static SingletonThree INSTANCE;

    /**
     * 3.获取实例方法
     *
     * @return
     */
    public static SingletonThree getInstance() {
        if (null == INSTANCE) {
            INSTANCE = new SingletonThree();
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        SingletonThree instance = SingletonThree.getInstance();
        System.out.println(instance);
    }
}

懒汉式(线程安全,同步方式)

package com.yundi.atp.dp.singleton;

/**
 * @Author: yanp
 * @Description: 懒汉式(线程安全,同步方式)
 * @Date: 2022/3/12 12:06
 * @Version: 1.0.0
 */
public class SingletonFour {
    /**
     * 1.私有化构造器
     */
    private SingletonFour() {

    }

    /**
     * 2.声明实例对象
     */
    private static SingletonFour INSTANCE;

    /**
     * 3.获取实例方法
     *
     * @return
     */
    public static synchronized SingletonFour getInstance() {
        if (null == INSTANCE) {
            INSTANCE = new SingletonFour();
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        SingletonFour instance = SingletonFour.getInstance();
        System.out.println(instance);
    }
}

双重检查

package com.yundi.atp.dp.singleton;

/**
 * @Author: yanp
 * @Description: 双重检查:保证了效率问题,线程安全,同时也实现了懒加载的效果
 * @Date: 2022/3/12 12:06
 * @Version: 1.0.0
 */
public class SingletonFive {
    /**
     * 1.私有化构造器
     */
    private SingletonFive() {

    }

    /**
     * 2.声明实例对象
     */
    private static volatile SingletonFive INSTANCE;

    /**
     * 3.获取实例方法
     *
     * @return
     */
    public static SingletonFive getInstance() {
        if (null == INSTANCE) {
            synchronized (SingletonFive.class) {
                if (null == INSTANCE) {
                    INSTANCE = new SingletonFive();
                }
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) {
        SingletonFive instance = SingletonFive.getInstance();
        System.out.println(instance);
    }
}

静态内部类

package com.yundi.atp.dp.singleton;

/**
 * @Author: yanp
 * @Description: 静态内部类:构造的时候并不会立即初始化,调用的时候才初始化.线程安全,实现了懒加载。
 * @Date: 2022/3/12 12:06
 * @Version: 1.0.0
 */
public class SingletonSix {
    /**
     * 1.私有化构造器
     */
    private SingletonSix() {

    }

    /**
     * 2.内部类创建实例对象
     */
    private static class Singleton {
        private final static SingletonSix INSTANCE = new SingletonSix();
    }

    /**
     * 3.获取实例方法
     *
     * @return
     */
    public static SingletonSix getInstance() {
        return Singleton.INSTANCE;
    }

    public static void main(String[] args) {
        SingletonSix instance = SingletonSix.getInstance();
        System.out.println(instance);
    }
}

枚举

package com.yundi.atp.dp.singleton;/**
 * @Author: yanp
 * @Description:
 * @Date: 2022/3/12 13:04
 * @Version: 1.0.0
 */

/**
 * @Author: yanp
 * @Description: 枚举
 * @Date: 2022/3/12 13:04
 * @Version: 1.0.0
 */
public enum SingletonSeven {
    INSTANCE;
    public void test1(){
        System.out.println("hello world!");
    }

    public static void main(String[] args) {
        SingletonSeven instance = SingletonSeven.INSTANCE;
        SingletonSeven instance1 = SingletonSeven.INSTANCE;
        System.out.println("instance:"+instance.hashCode());
        System.out.println("instance1:"+instance1.hashCode());

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

厉害哥哥吖

您的支持是我创作下去的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值