单例模式----懒汉设计(不推荐使用)

单例模式----懒汉式的三种实现方式

实现单例模式共有8中方法,其中不推荐使用,或者设计不合理的单例模式有以下三种

线程不安全式懒汉式
package com.design_patterns.singleton;

/**
 * 懒汉式(线程安全,但效率低下哦)
 */
class Person04{
    private static Person04 person = null;
    private Person04(){}                      //对构造方法进行私有化

    /**
     * 定义getInstance()静态方法,可以直接通过此类调用此方法获取此类的实例化对象
     * 此方法加入了synchronized关键字,因此不会出现线程安全问题
     * @return
     */
    public static synchronized Person04 getInstance(){
        if(person == null){             //判断person对象是否存在,若不存在为null,才进行创建
            person = new Person04();
        }
        return person;
    }
}


public class SingletonType04 {
    public static void main(String[] args) throws Exception {

        Person04 person = Person04.getInstance();          //根据Person04类中的静态方法获取实例
        Person04 person04 = Person04.getInstance();

        System.out.println("主线程Person04类的对象person是:\n" + person + "\n" + person.hashCode());
        System.out.println("主线程Person04类的对象person04是:\n" + person + "\n" + person.hashCode());

        System.out.println("这两个对象一样么?" + (person == person04));
    }
}


/**
 运行结果:

 主线程Person03类的对象person是:
 com.design_patterns.singleton.Person04@4554617c
 1163157884
 主线程Person04类的对象person04是:
 com.design_patterns.singleton.Person04@4554617c
 1163157884
 这两个对象一样么?true

 */

优缺点说明

在这里插入图片描述

线程安全懒汉式
package com.design_patterns.singleton;

/**
 * 懒汉式(线程不安全)
 * 错误的写法,不能采用此种方式
 */
class Person05{
    private static Person05 person = null;
    private Person05(){}                      //对构造方法进行私有化

    /**
     * 定义getInstance()静态方法,可以直接通过此类调用此方法获取此类的实例化对象
     * 但是当多个线程同时进行执行此方法时,就会有多个实例化对象产生,因此采用此种方法创建单例模式了是不安全的
     * @return
     */
    public static Person05 getInstance(){
        if(person == null){             //判断person对象是否存在,若不存在为null,才进行创建
            synchronized (Person05.class){          //将实例化操作放入代码块中,但是此种操作依然无法解决线程安全问题
                person = new Person05();
            }
        }
        return person;
    }
}


public class SingletonType05 {
    public static void main(String[] args) throws Exception {

        Person05 person = Person05.getInstance();          //根据Person04类中的静态方法获取实例
        Person05 person05 = Person05.getInstance();

        System.out.println("主线程Person05类的对象person是:\n" + person + "\n" + person.hashCode());
        System.out.println("主线程Person05类的对象person05是:\n" + person + "\n" + person.hashCode());

        System.out.println("这两个对象一样么?" + (person == person05));
    }
}

/**
 主线程Person05类的对象person是:
 com.design_patterns.singleton.Person05@4554617c
 1163157884
 主线程Person05类的对象person05是:
 com.design_patterns.singleton.Person05@4554617c
 1163157884
 这两个对象一样么?true

 */

优缺点说明

在这里插入图片描述

线程不安全懒汉式(使用错误)
package com.design_patterns.singleton;

/**
 * 懒汉式(线程不安全)
 * 错误的写法,不能采用此种方式
 */
class Person05{
    private static Person05 person = null;
    private Person05(){}                      //对构造方法进行私有化

    /**
     * 定义getInstance()静态方法,可以直接通过此类调用此方法获取此类的实例化对象
     * 但是当多个线程同时进行执行此方法时,就会有多个实例化对象产生,因此采用此种方法创建单例模式了是不安全的
     * @return
     */
    public static Person05 getInstance(){
        if(person == null){             //判断person对象是否存在,若不存在为null,才进行创建
            synchronized (Person05.class){          //将实例化操作放入代码块中,但是此种操作依然无法解决线程安全问题
                person = new Person05();
            }
        }
        return person;
    }
}


public class SingletonType05 {
    public static void main(String[] args) throws Exception {

        Person05 person = Person05.getInstance();          //根据Person04类中的静态方法获取实例
        Person05 person05 = Person05.getInstance();

        System.out.println("主线程Person05类的对象person是:\n" + person + "\n" + person.hashCode());
        System.out.println("主线程Person05类的对象person05是:\n" + person + "\n" + person.hashCode());

        System.out.println("这两个对象一样么?" + (person == person05));
    }
}

/**
 主线程Person05类的对象person是:
 com.design_patterns.singleton.Person05@4554617c
 1163157884
 主线程Person05类的对象person05是:
 com.design_patterns.singleton.Person05@4554617c
 1163157884
 这两个对象一样么?true

 */

优缺点说明

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Gaolw1102

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值