首页 > 其他分享 >单例模式的6种写法

单例模式的6种写法

时间:2023-02-14 09:56:19浏览次数:39  
标签:Singleton singleton private class static 模式 单例 写法 public

简单点说,就是一个应用程序中,某个类的实例对象只有一个,你没有办法去new,因为
构造器是被private修饰的,一般通过getInstance()的方法来获取它们的实例。
getInstance()的返回值是一个对象的引用,并不是一个新的实例,所以不要错误的理解
成多个对象。单例模式实现起来也很容易,直接看demo吧.


1. 懒汉写法(线程不安全)
public class Singleton {
private static Singleton singleton;
private Singleton() {
}
public static Singleton getInstance() {
if (singleton == null) {
singleton = new Singleton();
}
return singleton;
}
}


2. 懒汉式写法(线程安全)
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}


3. 饿汉式写法
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}


4. 静态内部类
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}


5. 枚举(这种方式是Effective Java作者Josh Bloch 提倡的方式,它不仅能避免多线程同步问
题,而且还能防止反序列化重新创建新的对象)
public enum Singleton {
INSTANCE;
public void whateverMethod() {
}
}


6. 双重校验锁
public class Singleton {
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}

标签:Singleton,singleton,private,class,static,模式,单例,写法,public
From: https://www.cnblogs.com/18sui/p/17118642.html

相关文章

  • 通过rlwrap实现disql、dmrman交互模式上下翻、左右移动光标功能
    前言DM数据库默认没有上下翻看历史SQL、左右移动光标修改SQL的功能,无论是在测试学习还是生产中不便利,接下来介绍如何通过rlwrap实现DM数据库中disql、dmrman交互模式上下......
  • 无头模式窗口大小导致的问题
    fromseleniumimportwebdriverfromselenium.webdriver.common.alertimportAlertfromselenium.webdriver.support.waitimportWebDriverWaitfromselenium.webdriv......
  • spark的DataFrame的schema模式:读时模式, 指定模式
    读时模式valpath="/Volumes/Data/BigData_code/data/"//读取json生成dataframevaldf=spark.read.format("json").load(path+"flight-data/json/2015-......
  • 文盘Rust -- 领域交互模式如何实现
    作者:京东科技贾世闻文盘Rust--领域交互模式如何实现书接上文,上回说到如何通过​​interactcli-rs​​四步实现一个命令行程序。但是shell交互模式在有些场景下用户体验并......
  • 文盘Rust -- 领域交互模式如何实现
    作者:京东科技贾世闻文盘Rust--领域交互模式如何实现书接上文,上回说到如何通过interactcli-rs四步实现一个命令行程序。但是shell交互模式在有些场景下用户体验并不是......
  • 代理设计模式
    代理设计模式​​1、代理设计模式​​​​1.1静态代理模式​​​​1.2动态代理模式​​1、代理设计模式  所谓代理,就是替别人完成一些事情。在Java开发中,我们也会遇到一......
  • axios的第二种写法,把请求路径抽离到一个文件
    utils/http.jsimportaxiosfrom'axios';importqsfrom'qs';import{VALID_LOGIN}from'_config/url'importcontextfrom'../main.js'importrouterfrom'........
  • Redis主从模式的优缺点
    优点:一个Master可以同步多个SlavesSlave同样可以接受其它Slaves的连接和同步请求,这样可以有效的分载Master的同步压力。因此我们可以将Redis的Replication架构视为图结......
  • 什么是设计模式?
    所谓设计模式,就是对经常出现的软件设计问题的成熟解决方案。很多人把设计模式想象成非常高深的概念,实际上设计模式仅仅是对特定问题的一种惯性思维。有些人喜欢抱着一本设......
  • vue3写法
    1、Vue3初始化写法变了data写法改了,函数式写法   2、router  路由重定向写法 ......