实现Singleton 模式——七种实现方式
题目:设计一个类,我们只能生成该类的一个实例
- public class Test02 {
- /**
- * 单例模式,懒汉式,线程安全
- */
- public static class Singleton {
- private final static Singleton INSTANCE = new Singleton();
- private Singleton() {
- }
- public static Singleton getInstance() {
- return INSTANCE;
- }
- }
- /**
- * 单例模式,饿汉式,线程不安全
- */
- public static class Singleton2 {
- private static Singleton2 instance = null;
- private Singleton2() {
- }
- public static Singleton2 getInstance() {
- if (instance == null) {
- instance = new Singleton2();
- }
- return instance;
- }
- }
- /**
- * 单例模式,饿汉式,线程安全,多线程环境下效率不高
- */
- public static class Singleton3 {
- private static Singleton3 instance = null;
- private Singleton3() {
- }
- public static synchronized Singleton3 getInstance() {
- if (instance == null) {
- instance = new Singleton3();
- }
- return instance;
- }
- }
- /**
- * 单例模式,懒汉式,变种,线程安全
- */
- public static class Singleton4 {
- private static Singleton4 instance = null;
- static {
- instance = new Singleton4();
- }
- private Singleton4() {
- }
- public static Singleton4 getInstance() {
- return instance;
- }
- }
- /**
- * 单例模式,使用静态内部类,线程安全【推荐】
- */
- public static class Singleton5 {
- private final static class SingletonHolder {
- private static final Singleton5 INSTANCE = new Singleton5();
- }
- private Singleton5() {
- }
- public static Singleton5 getInstance() {
- return SingletonHolder.INSTANCE;
- }
- }
- /**
- * 静态内部类,使用枚举方式,线程安全【推荐】
- */
- public enum Singleton6 {
- INSTANCE;
- public void whateverMethod() {
- }
- }
- /**
- * 静态内部类,使用双重校验锁,线程安全【推荐】
- */
- public static class Singleton7 {
- private volatile static Singleton7 instance = null;
- private Singleton7() {
- }
- public static Singleton7 getInstance() {
- if (instance == null) {
- synchronized (Singleton7.class) {
- if (instance == null) {
- instance = new Singleton7();
- }
- }
- }
- return instance;
- }
- }
- public static void main(String[] args) {
- System.out.println(Singleton.getInstance() == Singleton.getInstance());
- System.out.println(Singleton2.getInstance() == Singleton2.getInstance());
- System.out.println(Singleton3.getInstance() == Singleton3.getInstance());
- System.out.println(Singleton4.getInstance() == Singleton4.getInstance());
- System.out.println(Singleton5.getInstance() == Singleton5.getInstance());
- System.out.println(Singleton6.INSTANCE == Singleton6.INSTANCE);
- System.out.println(Singleton7.getInstance() == Singleton7.getInstance());
- }
- }
运行结果:
实现Singleton 模式——七种实现方式相关推荐
- 商业模式 七种_5种不同的在线商业模式
商业模式 七种 In the age of the internet, it's easier for everyone to become an entrepreneur. Setting up a ...
- 知识付费的七种变现方式
知识付费的七种变现方式. 一在线问答 以文字.音频.视频等方式来对提问者的问题进行回答.只要你在某些领域有丰富的知识积累,那么你的回答就能得到提问者的青睐,就可以赚取相应的佣金.不过这种收益方式效果甚 ...
- 【数据库视频】七种连接方式
数据表的查询与管理只是针对数据库中的一个表格进行的查询管理,如果现在我们想要同时的看到两个数据表中的数据的或,需要怎么实现?答案是:使用多连接的方式进行查询 标题中说了SQL中有七种连接的方式,那么具 ...
- 单例模式详解(七种实现方式)
定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式结构图: 单例模式有多种写法各有利弊,现在我们来看看各种模式写法. 1. 饿汉模式 public class Singleton ...
- JavaScript中七种函数调用方式及对应 this 的含义
http://blog.sina.com.cn/s/blog_621f1e120100rj21.html this 在 JavaScript 开发中占有相当重要的地位,不过很多人对this这个东西都感 ...
- 数据库的七种传播方式
其实数据库的七种传播行为在网上查询了一下,和Spring中使用到的7种事务传播行为是没什么差别的,因为我写的博文基本和JAVA有相关,所以就结合Spring中事务传播和例子来讲一下咯. 事务传播行为如 ...
- 设计模式之终结者模式(七种完整版)
五.结构型模式 结构型模式描述如何将类或对象按某种布局组成更大的结构.它分为类结构型模式和对象结构型模式,前者采用继承机制来组织接口和类,后者釆用组合或聚合来组合对象. 由于组合关系或聚合关系比继承关 ...
- java中singleton_java中singleton的几种实现方式
传统的最简单的方式 这种模式有一个缺点就是不能实现延迟加载,也就是当Singleton类被初始化时,INSTANCE立刻就被创建.比如这个类包含其它static方法,而你正好又调用了static方法, ...
- Javascript创建对象的七种常用方式
js常用的几种创建对象的方式有: 1.{} 2.new Object() 3.使用字面量 4.工厂模式 5.构造函数模式(constructor) 6.原型模式(prototype) 7.构造函数+原 ...
最新文章
- Python网络爬虫与信息提取(三)(正则表达式的基础语法)
- Gitlab搭建安装及使用中遇到的问题。
- hash算法的介绍 【清晰易懂】
- 一致性哈希和哈希槽对比
- 6LoWPAN Header compression
- 怎么看软件的编写代码
- 开发者的实用 Vim 插件(二)
- 任务管理器被管理员禁用如何解决
- 人工智能能否在翻译中胜过人类?
- java升序排列数组_java数组的升序降序排列
- JAVA中 BufferedImage、ImageIO用法
- 怎么开通商家转账到零钱?
- 在java中 int类型对应的包装类是_Java语言对简单数据类型进行了类包装,int对应的包装类是______。...
- SNH48周边商品抢购分析
- 会员营销体系中,促成会员转化的关注点有哪些
- CSS技巧性实现多边形及各种条纹渐变图形
- Portals G【并查集】
- 使用jdbc连接orcal数据库(完整教程)
- 每日分享html特效篇之五个加载页面特效和五个导航按钮特效
- 在IDEA中,自主导出jar包
热门文章
- 【Android 逆向】ELF 文件格式总结 ★★★
- 【Java 并发编程】线程池机制 ( 线程池阻塞队列 | 线程池拒绝策略 | 使用 ThreadPoolExecutor 自定义线程池参数 )
- 【运筹学】表上作业法 ( 找初始基可行解 | 计算检验数 | 调整运量 )
- 洛谷 P4151 BZOJ 2115 [WC2011]最大XOR和路径
- [FJOI2007]轮状病毒
- 随笔18 java中的类加载器
- poj 1379 模拟退火法
- 从无头单链表中删除节点 结构之法 4
- adb shell root
- 每3位新码农中就有2个是单身?来自31000人的调查报告显示……