Java设计模式——单例模式

本文详细介绍了单例模式的概念,如何通过构造函数私有化和静态方法实现单例,并针对多线程环境下的并发控制进行讨论。通过实例代码演示了单例模式在实际开发中的应用。

单例模式

单例模式是一种创建型设计模式,让你能够保证一个类只有一个实例,并提供一个访问该节点的全局节点。

问题

1.保证一个类只有一个实例
2.为该实例提供一个全局访问节点。

解决办法

所有单例的实现都包含一下两个相同的步骤:

  • 将默认构造函数设为私有,防止其他对象使用单例类的new运算符。
  • 新建一个静态构造方法作为构造函数。该函数会偷偷调用私有构造函数来创建对象,
    并将其保存在一个静态成员变量中。此后所有对于该函数的调用都将返回这一缓存对象。

单例模式结构

1.单例(Singleton)类声明了一个名为getInstance(获取实例)的静态方法来返回其所属的一个相同实例。

单例的构造函数必须对客户端代码隐藏。调用获取实例方法必须是获取单例对象的唯一方式。

代码实现

  • Singleton
package com.wl.singleton;

/**
 * @Author Mr_wan
 * @Description TODO $
 * @Date $ 2021.09.01$
 * @Param 单例模式——单线程$
 * @return $
 */
public final class Singleton {
    private static Singleton instance;
    public String value;
    private Singleton(String value) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.value = value;
    }
    public static Singleton getInstance(String value){
        if (instance == null) {
            instance = new Singleton(value);
        }
        return instance;
    }
}

基础单例(单线程)

package com.wl.singleton;

/**
 * @Author Mr_wan
 * @Description TODO $
 * @Date $ 2021.09.01$
 * @Param 单例模式——测试$
 * @return $
 */
public class DemoSingleThread {
    public static void main(String[] args) {
        System.out.println("If you see the same value, then singleton was reused (yay!)" + "\n" +
                "If you see different values, then 2 singletons were created (booo!!)" + "\n\n" +
                "RESULT:" + "\n");
        Singleton singleton = Singleton.getInstance("first");
        Singleton otherSing = Singleton.getInstance("other");
        System.out.println(singleton.value);
        System.out.println(otherSing.value);
    }
}

###基础单例(多线程)

package com.wl.singleton;


/**
 * @Author Mr_wan
 * @Description TODO $
 * @Date $ 2021.09.01$
 * @Param 单例模式——多线程$
 * @return $
 */
public class DemoMultiTread {
    public static void main(String[] args) {
        System.out.println("If you see the same value, then singleton was reused (yay!)" + "\n" +
                "If you see different values, then 2 singletons were created (booo!!)" + "\n\n" +
                "RESULT:" + "\n");
        Thread threadFirst = new Thread(new ThreadFoo());
        Thread threadOther = new Thread(new ThreadBar());
        threadFirst.start();
        threadOther.start();
    }
    static class ThreadFoo implements Runnable {

        @Override
        public void run() {
            Singleton singleton = Singleton.getInstance("first");
            System.out.println(singleton.value);
        }
    }
    static class ThreadBar implements Runnable{

        @Override
        public void run() {
            Singleton singleton = Singleton.getInstance("other");
            System.out.println(singleton.value);
        }
    }

}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值