题目:设计一个类,我们只能生成该类的一个实例

[java] view plaincopyprint?
  1. public class Test02 {
  2. /**
  3. * 单例模式,懒汉式,线程安全
  4. */
  5. public static class Singleton {
  6. private final static Singleton INSTANCE = new Singleton();
  7. private Singleton() {
  8. }
  9. public static Singleton getInstance() {
  10. return INSTANCE;
  11. }
  12. }
  13. /**
  14. * 单例模式,饿汉式,线程不安全
  15. */
  16. public static class Singleton2 {
  17. private static Singleton2 instance = null;
  18. private Singleton2() {
  19. }
  20. public static Singleton2 getInstance() {
  21. if (instance == null) {
  22. instance = new Singleton2();
  23. }
  24. return instance;
  25. }
  26. }
  27. /**
  28. * 单例模式,饿汉式,线程安全,多线程环境下效率不高
  29. */
  30. public static class Singleton3 {
  31. private static Singleton3 instance = null;
  32. private Singleton3() {
  33. }
  34. public static synchronized Singleton3 getInstance() {
  35. if (instance == null) {
  36. instance = new Singleton3();
  37. }
  38. return instance;
  39. }
  40. }
  41. /**
  42. * 单例模式,懒汉式,变种,线程安全
  43. */
  44. public static class Singleton4 {
  45. private static Singleton4 instance = null;
  46. static {
  47. instance = new Singleton4();
  48. }
  49. private Singleton4() {
  50. }
  51. public static Singleton4 getInstance() {
  52. return instance;
  53. }
  54. }
  55. /**
  56. * 单例模式,使用静态内部类,线程安全【推荐】
  57. */
  58. public static class Singleton5 {
  59. private final static class SingletonHolder {
  60. private static final Singleton5 INSTANCE = new Singleton5();
  61. }
  62. private Singleton5() {
  63. }
  64. public static Singleton5 getInstance() {
  65. return SingletonHolder.INSTANCE;
  66. }
  67. }
  68. /**
  69. * 静态内部类,使用枚举方式,线程安全【推荐】
  70. */
  71. public enum Singleton6 {
  72. INSTANCE;
  73. public void whateverMethod() {
  74. }
  75. }
  76. /**
  77. * 静态内部类,使用双重校验锁,线程安全【推荐】
  78. */
  79. public static class Singleton7 {
  80. private volatile static Singleton7 instance = null;
  81. private Singleton7() {
  82. }
  83. public static Singleton7 getInstance() {
  84. if (instance == null) {
  85. synchronized (Singleton7.class) {
  86. if (instance == null) {
  87. instance = new Singleton7();
  88. }
  89. }
  90. }
  91. return instance;
  92. }
  93. }
  94. public static void main(String[] args) {
  95. System.out.println(Singleton.getInstance() == Singleton.getInstance());
  96. System.out.println(Singleton2.getInstance() == Singleton2.getInstance());
  97. System.out.println(Singleton3.getInstance() == Singleton3.getInstance());
  98. System.out.println(Singleton4.getInstance() == Singleton4.getInstance());
  99. System.out.println(Singleton5.getInstance() == Singleton5.getInstance());
  100. System.out.println(Singleton6.INSTANCE == Singleton6.INSTANCE);
  101. System.out.println(Singleton7.getInstance() == Singleton7.getInstance());
  102. }
  103. }

运行结果:

实现Singleton 模式——七种实现方式相关推荐

  1. 商业模式 七种_5种不同的在线商业模式

    商业模式 七种 In the age of the internet, it's easier for everyone to become an entrepreneur. Setting up a ...

  2. 知识付费的七种变现方式

    知识付费的七种变现方式. 一在线问答 以文字.音频.视频等方式来对提问者的问题进行回答.只要你在某些领域有丰富的知识积累,那么你的回答就能得到提问者的青睐,就可以赚取相应的佣金.不过这种收益方式效果甚 ...

  3. 【数据库视频】七种连接方式

    数据表的查询与管理只是针对数据库中的一个表格进行的查询管理,如果现在我们想要同时的看到两个数据表中的数据的或,需要怎么实现?答案是:使用多连接的方式进行查询 标题中说了SQL中有七种连接的方式,那么具 ...

  4. 单例模式详解(七种实现方式)

    定义:保证一个类仅有一个实例,并提供一个访问它的全局访问点. 单例模式结构图: 单例模式有多种写法各有利弊,现在我们来看看各种模式写法. 1. 饿汉模式 public class Singleton ...

  5. JavaScript中七种函数调用方式及对应 this 的含义

    http://blog.sina.com.cn/s/blog_621f1e120100rj21.html this 在 JavaScript 开发中占有相当重要的地位,不过很多人对this这个东西都感 ...

  6. 数据库的七种传播方式

    其实数据库的七种传播行为在网上查询了一下,和Spring中使用到的7种事务传播行为是没什么差别的,因为我写的博文基本和JAVA有相关,所以就结合Spring中事务传播和例子来讲一下咯. 事务传播行为如 ...

  7. 设计模式之终结者模式(七种完整版)

    五.结构型模式 结构型模式描述如何将类或对象按某种布局组成更大的结构.它分为类结构型模式和对象结构型模式,前者采用继承机制来组织接口和类,后者釆用组合或聚合来组合对象. 由于组合关系或聚合关系比继承关 ...

  8. java中singleton_java中singleton的几种实现方式

    传统的最简单的方式 这种模式有一个缺点就是不能实现延迟加载,也就是当Singleton类被初始化时,INSTANCE立刻就被创建.比如这个类包含其它static方法,而你正好又调用了static方法, ...

  9. Javascript创建对象的七种常用方式

    js常用的几种创建对象的方式有: 1.{} 2.new Object() 3.使用字面量 4.工厂模式 5.构造函数模式(constructor) 6.原型模式(prototype) 7.构造函数+原 ...

最新文章

  1. Python网络爬虫与信息提取(三)(正则表达式的基础语法)
  2. Gitlab搭建安装及使用中遇到的问题。
  3. hash算法的介绍 【清晰易懂】
  4. 一致性哈希和哈希槽对比
  5. 6LoWPAN Header compression
  6. 怎么看软件的编写代码
  7. 开发者的实用 Vim 插件(二)
  8. 任务管理器被管理员禁用如何解决
  9. 人工智能能否在翻译中胜过人类?
  10. java升序排列数组_java数组的升序降序排列
  11. JAVA中 BufferedImage、ImageIO用法
  12. 怎么开通商家转账到零钱?
  13. 在java中 int类型对应的包装类是_Java语言对简单数据类型进行了类包装,int对应的包装类是______。...
  14. SNH48周边商品抢购分析
  15. 会员营销体系中,促成会员转化的关注点有哪些
  16. CSS技巧性实现多边形及各种条纹渐变图形
  17. Portals G【并查集】
  18. 使用jdbc连接orcal数据库(完整教程)
  19. 每日分享html特效篇之五个加载页面特效和五个导航按钮特效
  20. 在IDEA中,自主导出jar包

热门文章

  1. 【Android 逆向】ELF 文件格式总结 ★★★
  2. 【Java 并发编程】线程池机制 ( 线程池阻塞队列 | 线程池拒绝策略 | 使用 ThreadPoolExecutor 自定义线程池参数 )
  3. 【运筹学】表上作业法 ( 找初始基可行解 | 计算检验数 | 调整运量 )
  4. 洛谷 P4151 BZOJ 2115 [WC2011]最大XOR和路径
  5. [FJOI2007]轮状病毒
  6. 随笔18 java中的类加载器
  7. poj 1379 模拟退火法
  8. 从无头单链表中删除节点 结构之法 4
  9. adb shell root
  10. 每3位新码农中就有2个是单身?来自31000人的调查报告显示……