概述
- 单例模式是一种常用的软件设计模式,其定义是单例对象的类只能允许一个实例存在。
- 许多时候,整个系统只需要用户一个的全局对象,这样有利于我们协调系统整体的行为,比如在某个服务器程序中,该服务器的配置信息存放在一个文件中,这些配置数据由一个单例对象统一读取,然后服务进程中的其他对象再通过这个单例对象获取这些配置信息。这种方式简化了在复杂环境下的配置管理。
基本的实现思路
- 单例模式要求类能够有返回对象一个引用(永远是同一个)和一个获得该实例的方法(必须是静态方法,通常使用getInstance这个名称)。
- 步骤一:将该类的构造方法定义为私有方法,这样其他的代码就无法通过调用该类的构造方法来实例化该类的对象,只有通过该类提供的静态方法来得到该类的唯一实例。
- 步骤二:在该类内提供一个静态方法,当我们调用这个方法时,如果类持有的引用不为空就返回这个引用,如果类保持的引用为空就创建该类的实例并将实例的引用赋予该类保持的引用。
具体写法
-
饿汉式
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public class Singleton { 2 3 private final static Singleton INSTANCE = new Singleton(); 4 5 private Singleton(){} 6 7 public static Singleton getInstance(){ 8 return INSTANCE; 9 } 10 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public class Singleton { 2 3 private static Singleton instance; 4 5 static { 6 instance = new Singleton(); 7 } 8 9 private Singleton() {} 10 11 public static Singleton getInstance() { 12 return instance; 13 } 14 }
优点:写法简单,就是在类装载的时候就完成实例化,避免线程同步问题。
缺点:在类装载的时候就完成实例化,没有达到Lazy Loading的效果,如果从始至终从未使用过这个实例,则会造成内存的浪费。
-
懒汉式
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public class Singleton { 2 3 private static Singleton singleton; 4 5 private Singleton() {} 6 7 public static Singleton getInstance() { 8 if (singleton == null) { 9 singleton = new Singleton(); 10 } 11 return singleton; 12 } 13 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public class Singleton { 2 3 private static Singleton singleton; 4 5 private Singleton() {} 6 7 public static synchronized Singleton getInstance() { 8 if (singleton == null) { 9 singleton = new Singleton(); 10 } 11 return singleton; 12 } 13 }
缺点:效率太低,每个线程在想获得类的实例的时候,执行getInstance()方法都要进行同步。而其实这个方法只执行一次实例化代码就够了,后面的想要获得该实例,直接return就行了。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public class Singleton { 2 3 private static Singleton singleton; 4 5 private Singleton() {} 6 7 public static Singleton getInstance() { 8 if (singleton == null) { 9 synchronized (Singleton.class) { 10 singleton = new Singleton(); 11 } 12 } 13 return singleton; 14 } 15 }
缺点:跟上一种实现方式遇到的情形一致,假如一个线程进入了if (singleton == null)判断语句块,还未来得及往下执行,另一个线程也通过了这个判断语句,这时便会产生多个实例。
-
双重检查(推荐用)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public class Singleton { 2 3 private static volatile Singleton singleton; 4 5 private Singleton() {} 6 7 public static Singleton getInstance() { 8 if (singleton == null) { 9 synchronized (Singleton.class) { 10 if (singleton == null) { 11 singleton = new Singleton(); 12 } 13 } 14 } 15 return singleton; 16 } 17 }
优点:线程安全,延迟加载,效率较高
-
静态内部类(推荐用)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public class Singleton { 2 3 private Singleton() {} 4 5 private static class SingletonInstance { 6 private static final Singleton INSTANCE = new Singleton(); 7 } 8 9 public static Singleton getInstance() { 10 return SingletonInstance.INSTANCE; 11 } 12 }
饿汉式方式是只要Singleton类被装载就会被实例化,没有Lazy-Loading的作用,而静态内部类方式在Singleton类被装载时并不会立即实例化,而是在需要实例化时,调用getInstance()方法,才会装载SingletonInstance类,从而完成Singleton的实例化。
类的静态属性只会在加载类的时候初始化,所以,JVM保证了线程的安全性,在类进行初始化时,别的线程是无法进入的。
优点:避免了线程不安全,延迟加载,效率高。
-
枚举
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 public enum Singleton { 2 INSTANCE; 3 public void whateverMethod() { 4 5 } 6 }
借助JDK1.5中添加的枚举来实现单例模式。不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。可能是因为枚举在JDK1.5中才添加,所以在实际项目开发中,很少见人这么写过。
优点:系统内存中该类只存在一个对象,节省了系统资源,对于一些需要频繁创建销毁的对象,使用单例模式可以提高系统性能。
缺点:当想实例化一个单例类的时候,必须要记住使用相应的获取对象的方法,而不是使用new,可能会给其他开发人员造成困扰,特别是看不到源码的时候。
适用场合:
-
- 需要频繁的进行创建和销毁的对象
- 创建对象时耗时过多或耗费资源过多,但又经常用到的对象
- 工具类对象
- 频繁访问数据数据库或文件的对象
多例模式
定义
- 作为对象的创建模式,多例模式中的多例类可以有多个实例,而且多例类必须自己创建、管理自己的实例,并向外界提供自己的实例。
特点
- 多例类可有多个实例。
- 多例类必须自己创建,管理自己的实例,并向外界提供自己的实例。
- 根据是否有实例上限分为:有上限多例类和无上限多例类。
具体实现
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.hmz; 2 3 import java.util.ArrayList; 4 import java.util.Random; 5 6 public class Emperor { 7 8 private static int maxNumOfEmperor = 2; // 最多只能有连个皇帝 9 private static ArrayList emperorInfoList = new ArrayList(maxNumOfEmperor); // 皇帝叫什么名字 10 private static ArrayList emperorList = new ArrayList(maxNumOfEmperor); // 装皇帝的列表; 11 private static int countNumOfEmperor = 0; // 正在被人尊称的是那个皇帝 12 // 先把2个皇帝产生出来 13 static { 14 // 把所有的皇帝都产生出来 15 for (int i = 0; i < maxNumOfEmperor; i++) { 16 emperorList.add(new Emperor("皇" + (i + 1) + "帝")); 17 } 18 } 19 20 // 就这么多皇帝了,不允许再推举一个皇帝(new 一个皇帝) 21 private Emperor() { 22 // 世俗和道德约束你,目的就是不让你产生第二个皇帝 23 } 24 25 private Emperor(String info) { 26 emperorInfoList.add(info); 27 } 28 29 public static Emperor getInstance() { 30 Random random = new Random(); 31 countNumOfEmperor = random.nextInt(maxNumOfEmperor); // 随机拉出一个皇帝,只要是个精神领袖就成 32 return (Emperor) emperorList.get(countNumOfEmperor); 33 } 34 35 // 皇帝叫什么名字呀 36 public static void emperorInfo() { 37 System.out.println(emperorInfoList.get(countNumOfEmperor)); 38 } 39 40 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.hmz; 2 3 public class Minister { 4 5 public static void main(String[] args) { 6 int ministerNum =10; //10个大臣 7 for(int i=0;i<ministerNum;i++){ 8 Emperor emperor = Emperor.getInstance(); 9 System.out.print("第"+(i+1)+"个大臣参拜的是:"); 10 emperor.emperorInfo(); 11 } 12 } 13 14 }