Head First Design Patterns——单例模式

爱被打了一巴掌 2023-07-24 03:11 128阅读 0赞

文章目录

    • 1)饿汉式(静态常量)
    • 2)饿汉式(静态代码块)
    • 3)懒汉式(线程不安全)
    • 4)懒汉式(线程安全,同步方法)
    • 5)懒汉式(线程安全,同步代码块)
    • 6)双重检查(推荐使用)
    • 7)静态内部类(推荐使用)
    • 8)枚举方式

单例模式:也叫单件模式,可以用来创建独一无二的,只能有一个实例的对象,并且该类只提供一个取得其对象实例的方法(static)!它是所有设计模式中最简单的,它的类图只有一个类:

1)饿汉式(静态常量)

创建的步骤:
1.构造器私有化(防止 new来获得实例)
2.类的内部创建对象
3.向外界暴露一个静态的公共方法。getInstance
4.代码实现

  1. //饿汉式(静态变量)
  2. class Singleton{
  3. //1.构造器私有化
  4. private Singleton(){
  5. }
  6. //2.本类内部创建对象实例
  7. private final static Singleton instance = new Singleton();
  8. //3.提供一个公有的静态方法,返回实例对象
  9. public static Singleton getInstance(){
  10. return instance;
  11. }
  12. }
  13. public class Singleton1 {
  14. public static void main(String[] args) {
  15. Singleton instance = Singleton.getInstance();
  16. Singleton instance1 = Singleton.getInstance();
  17. System.out.println("instance:"+instance.hashCode()+"\n"+"instance1:"+instance1.hashCode());
  18. }
  19. }
  • 测试结果
    在这里插入图片描述
    两个对象实例的hashCode值相等,说明 new的两个对象是同一个,这样就避免了new实例多个对象!
  • 优缺点:

优点:这种写法比较简单,就是在类装载的时候就完成 了实例化,避免了线程同步问题。
缺点:
1.在类装载时就完成 了实例化,油源有达到Lazy Loading的效果,如果从始至终从未使用过这个实例,则会造成内存的浪费。
2.这种方式基于classLoader机制避免了多线程的同步问题,不过,Instance在类装载时就实例化,在单例模式中大多数都是调用getInstance方法,但是导致类装载的原因有很多种,因此不能确定有其他的方式导致类装载,这时候初始化instance就没有达到Lazy Loading的效果。

  • 结论:

这种单例模式可用,可能会造成内存浪费


2)饿汉式(静态代码块)

这和方式和静态常量方法类似 ,只是在内部将创建实例对象的操作放到了静态代码块中进行了

  • 代码实现

    //饿汉式(静态代码块)

    class Singleton{

    1. //1.构造器私有化
    2. private Singleton(){
    3. }
    4. //2.本类内部创建对象实例
    5. private final static Singleton instance;
    6. static { //在静态代码块中创建单例对象
    7. instance = new Singleton();
    8. }
    9. //3.提供一个公有的静态方法,返回实例对象
    10. public static Singleton getInstance(){
    11. return instance;
    12. }

    }

    public class Singleton2 {

    1. public static void main(String[] args) {
    2. Singleton instance = Singleton.getInstance();
    3. Singleton instance1 = Singleton.getInstance();
    4. System.out.println("instance:"+instance.hashCode()+"\n"+"instance1:"+instance1.hashCode());
    5. }

    }

  • 运行结果
    在这里插入图片描述

  • 结论:

这种单例模式也是可用的,但是可能会造成内存浪费


3)懒汉式(线程不安全)

  • 代码实现

    public class SingletonTest03 {

    1. public static void main(String[] args) {
    2. Singleton instance = Singleton.getInstance();
    3. Singleton instance1 = Singleton.getInstance();
    4. System.out.println(instance == instance1);
    5. System.out.println("instance:"+instance.hashCode()+"\n"+"instance1:"+instance1.hashCode());
    6. }

    }

  1. class Singleton{
  2. private static Singleton instance;
  3. private Singleton(){ }
  4. //提供一个静态的公有方法,使用到该方法时,才去创建instance
  5. public static Singleton getInstance(){
  6. if (instance == null) {
  7. instance = new Singleton();
  8. }
  9. return instance;
  10. }
  11. }
  • 运行结果
    在这里插入图片描述
  • 总结:

提供一个静态的公有方法,使用到该方法时,才去创建instance
1.起到了Lazy Loading的效果,但是只能在单线程下使用
2.如果在多线程下,一个线程进入了if(singleton == null)判断语句时,还未来得及往下执行,另一个线程也通过了这个判断语句,这时便会产生多个实例。所以在多线程环境下不可使用这种方式

  • 结论:

在实际开发中,不要使用这种方式。


4)懒汉式(线程安全,同步方法)

  • 代码实现

    public class SingletonTest04 {

    1. public static void main(String[] args) {
    2. System.out.println("懒汉式(线程安全,同步方法)");
    3. Singleton instance = Singleton.getInstance();
    4. Singleton instance1 = Singleton.getInstance();
    5. System.out.println(instance == instance1);
    6. System.out.println("instance:"+instance.hashCode()+"\n"+"instance1:"+instance1.hashCode());
    7. }

    }

    //懒汉式(线程安全,同步方法)
    class Singleton{

    1. private static Singleton instance;
    2. private Singleton(){ }
    3. //提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题
    4. //synchronized
    5. public static synchronized Singleton getInstance(){
    6. if (instance == null) {
    7. instance = new Singleton();
    8. }
    9. return instance;
    10. }

    }

  • 运行结果
    在这里插入图片描述

  • 总结:
  1. 解决了线程不安全问题
  2. 效率太低了,每个线程都在想获得类的实例时候,执行getInstance()方法都要进行执行同步。而其实这个方法只执行一次实例化代码就够了,后面的想获得该实例,直接return 就行了,方法进行同步的效率太低了。

    • 总结
      在实际开发中,不推荐使用这种方式

5)懒汉式(线程安全,同步代码块)

  1. public class SingletonTest05 {
  2. public static void main(String[] args) {
  3. System.out.println("懒汉式 同步代码块");
  4. Singleton instance = Singleton.getInstance();
  5. Singleton instance1 = Singleton.getInstance();
  6. System.out.println(instance == instance1);
  7. System.out.println("instance:"+instance.hashCode()+"\n"+"instance1:"+instance1.hashCode());
  8. }
  9. }
  10. //懒汉式(线程安全,同步方法)
  11. class Singleton{
  12. private static Singleton instance;
  13. private Singleton(){ }
  14. //提供一个静态的公有方法,加入同步处理的代码,解决线程安全问题
  15. //synchronized
  16. public static Singleton getInstance(){
  17. if (instance == null) {
  18. synchronized (Singleton.class) {
  19. instance = new Singleton();
  20. }
  21. }
  22. return instance;
  23. }
  24. }
  • 运行结果
    在这里插入图片描述
  • 总结:
  1. 这种方式,本意是想对第四种方式的改进,因为前面同步方法效率太低了,改为同步 产生实例化的代码块。
  2. 但是这种同步并不能起到线程同步的作用。,假如一个线程进入到if(Singleton == null)判断语句块,还未来得及往下执行,另一个线程也通过了这个判断语句,这时便会产生多个实例。

    • 结论:
      在实际开发中,不能使用这种方式。

6)双重检查(推荐使用)

  • 代码实现

    public class SingletonTest06 {

    1. public static void main(String[] args) {
    2. System.out.println("双重检查");
    3. Singleton instance = Singleton.getInstance();
    4. Singleton instance1 = Singleton.getInstance();
    5. System.out.println(instance == instance1);
    6. System.out.println("instance:"+instance.hashCode()+"\n"+"instance1:"+instance1.hashCode());
    7. }

    }

    //懒汉式(线程安全,同步方法)
    class Singleton{

    1. private static volatile Singleton instance;
    2. private Singleton(){ }
    3. //提供一个静态的公有方法,加入双重检查代码,解决线程安全问题,同时解决懒加载问题
    4. //同时保证了效率
    5. public static Singleton getInstance(){
    6. if (instance == null) {
    7. synchronized (Singleton.class) {
    8. if (instance == null){
    9. instance = new Singleton();
    10. }
    11. }
    12. }
    13. return instance;
    14. }

    }

  • 运行结果
    在这里插入图片描述

  • 总结
  1. Double-Check概念是多线程开发中学使用到的,我们进行了两次if (instance == null)检查,这样就可以保证线程安全了
  2. 这样,实例化代码只执行一次,后面再次访问时,判断if (instance == null),直接 return实例化对象,也就避免了反复执行方法同步 。
  3. 线程安全,延迟加载,效率较高

    • 结论:
      在开发中,推荐使用这种单例设计模式!

7)静态内部类(推荐使用)

基本介绍:

  1. 静态内部类不会在类装载时,直接执行,保证了懒加载
  2. 在调用类中的方法时,再装载静态内部类,是可行的,JVM中进行类装载时是线程安全的

    • 代码实现

    public class SingletonTest7 {

    1. public static void main(String[] args) {
    2. System.out.println("静态内部类:单例模式");
    3. Singleton instance = Singleton.getInstance();
    4. Singleton instance1 = Singleton.getInstance();
    5. System.out.println(instance == instance1);
    6. System.out.println("instance:"+instance.hashCode()+"\n"+"instance1:"+instance1.hashCode());
    7. }

    }

    //静态内部类
    class Singleton{

    1. private static volatile Singleton instance;
    2. private Singleton(){ }
    3. //静态内部类
    4. private static class SingletonInstance{
    5. private static final Singleton INSTANCE = new Singleton();
    6. }
    7. //提供一个静态的公有方法,直接返回SingletonInstance.INSTANCE
    8. public static Singleton getInstance(){
    9. return SingletonInstance.INSTANCE;
    10. }

    }

    • 运行结果
      在这里插入图片描述
    • 总结

1.这种方式采用了类装载的机制来保证初始化实例时只有一个线程。
2.静态内部类方式在Singleton类被装载时并不会立即实例化,而是在需要实例化时,调用getInstance方法,才会被装载SingletonInstance类,从而完成Singleton的实例化。
3.类的静态属性只会在第一次加载类的时候初始化,所以在这里,JVM帮助我们保证了线程的安全性,在类进行初始化时,别的线程是无法进入的。
4.优点:避免了线程不安全,利用静态内部类特点实现延迟加载,效率高。

  • 结论
    推荐使用!

8)枚举方式

  • 代码实现

    public class SingletonTest08 {

    1. public static void main(String[] args) {
    2. Singleton instance = Singleton.INSTANCE;
    3. Singleton instance1 = Singleton.INSTANCE;
    4. System.out.println(instance == instance1);
    5. System.out.println(instance.hashCode());
    6. System.out.println(instance1.hashCode());
    7. instance.sayHello();
    8. }

    }

    //使用枚举可以实现单例,推荐作用
    enum Singleton{

    1. INSTANCE;
    2. public void sayHello(){
    3. System.out.println("hello enum");
    4. }

    }

  • 运行结果
    在这里插入图片描述

  • 总结:

1.借助JDK1.5中添加的枚举来实现单例模式,不仅可以避免多线程同步 问题,而且还能防止反序列化重新创建新的对象

  • 结论:
    强力推荐使用!!!

在这里插入图片描述

整理不易,如果大家觉得好的话,可以加关注,后续会整理更多的知识点!

发表评论

表情:
评论列表 (有 0 条评论,128人围观)

还没有评论,来说两句吧...

相关阅读