前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 单例模式

Java 单例模式

作者头像
用户1180017
发布2018-06-21 16:58:22
7270
发布2018-06-21 16:58:22
举报
文章被收录于专栏:猿说1024猿说1024

单例模式

Singleton是一种创建型模式,指某个类采用Singleton模式,则在这个类被创建后,只可能产生一个实例供外部访问,并且提供一个全局的访问点。

核心知识点如下:

(1) 将采用单例设计模式的类的构造方法私有化(采用private修饰)。

(2) 在其内部产生该类的实例化对象,并将其封装成private static类型。 (3) 定义一个静态方法返回该类的实例。

示例代码:

代码语言:javascript
复制
package top.annwz.test;

/**
 * 单例模式
 *
 * Created by huahui.wu on 2017/4/10.
 */
public class SingletonTest {
}

/**
 * 懒汉模式
 */
class Singleton1{
    // 定义私有构造方法(防止通过 new SingletonTest()去实例化)
    private Singleton1() {}

    private static Singleton1 singleton1 = null;

    public static Singleton1 getInstance() {
        if (singleton1 == null ) {
            singleton1 = new Singleton1();

        }
        return singleton1;

    }
}


/**
 * 线程安全 但是效率不高
 */
class Singleton1Thread{
    private Singleton1Thread(){}

    private static Singleton1Thread instance = null;

    // 定义一个静态的方法(调用时再初始化Singleton1Thread,使用synchronized 避免多线程访问时,可能造成重的复初始化问题)
    public synchronized static Singleton1Thread getInstance() {
        //
        if (instance == null) {
            instance = new Singleton1Thread();
        }
        return instance;
    }
}

/**
 * 线程安全
 */
class Singleton2Thread{
    private Singleton2Thread(){}

    private static Singleton2Thread instance = null;

    public static Singleton2Thread getInstance() {
        // 对象实例化时与否判断(不使用同步代码块,instance不等于null时,直接返回对象,提高运行效率)
        if (instance == null) {
            //同步代码块(对象未初始化时,使用同步代码块,保证多线程访问时对象在第一次创建后,不再重复被创建)
            synchronized (SingletonTest.class) {
                //未初始化,则初始instance变量
                if (instance == null) {
                    instance = new Singleton2Thread();
                }
            }
        }
        return instance;
    }
}

/**
 * 饿汉模式
 */
class Singleton2{
    private Singleton2(){}

    private static Singleton2 instance = new Singleton2();

    public static Singleton2 getInstance() {
        return instance;
    }
}

/**
 * 用final 关键字修饰
 * 注解:定义一个私有的内部类,在第一次用这个嵌套类时,会创建一个实例。而类型为SingletonHolder的类,只有在Singleton.getInstance()中调用,由于私有的属性,他人无法使用SingleHolder,不调用Singleton.getInstance()就不会创建实例。
 * 优点:达到了lazy loading的效果,即按需创建实例。
 */
class Singleton3{
    private Singleton3(){}

    private static final Singleton3 instance = new Singleton3();

    public static Singleton3 getInstance() {
        return instance;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档