zoukankan      html  css  js  c++  java
  • 【设计模式系列】之单利模式

    1  概述

    本章主要与大家分享【设计模式系列】之单利模式内容,结合具体代码与大家一起分享。

    2  具体讲解

    2.1   结合代码分析

        第一种(懒汉,线程不安全):        

    public class Singleton {  
        private static Singleton instance;  
        private Singleton (){}  
      
        public static Singleton getInstance() {  
        if (instance == null) {  
            instance new Singleton();  
        }  
        return instance;  
        }  
    }  

          第二种(懒汉,线程安全):

    public class Singleton {  
        private static Singleton instance;  
        private Singleton (){}  
        public static synchronized Singleton getInstance() {  
        if (instance == null) {  
            instance new Singleton();  
        }  
        return instance;  
        }  
    }  

          第三种(饿汉):

    1 public class Singleton {  
    2     private static Singleton instance = new Singleton();  
    3     private Singleton (){}  
    4     public static Singleton getInstance() {  
    5     return instance;  
    6     }  
    7 }  

       第四种(饿汉,变种):

    public class Singleton {  
        private Singleton instance = null;  
        static {  
        instance new Singleton();  
        }  
        private Singleton (){}  
        public static Singleton getInstance() {  
        return this.instance;  
        }  
    }  

        第五种(静态内部类):

    1 public class Singleton {  
    2     private static class SingletonHolder {  
    3     private static final Singleton INSTANCE = new Singleton();  
    4     }  
    5     private Singleton (){}  
    6     public static final Singleton getInstance() {  
    7     return SingletonHolder.INSTANCE;  
    8     }  
    9 }  

        第六种(枚举):

    1 public enum Singleton {  
    2     INSTANCE;  
    3     public void whateverMethod() {  
    4     }  
    5 }  

        第七种(双重校验锁):

     1 public class Singleton {  
     2     private volatile static Singleton singleton;  
     3     private Singleton (){}  
     4     public static Singleton getSingleton() {  
     5     if (singleton == null) {  
     6         synchronized (Singleton.class) {  
     7         if (singleton == null) {  
     8             singleton = new Singleton();  
     9         }  
    10         }  
    11     }  
    12     return singleton;  
    13     }  
    14 }  

    2.2 总结

    (1)单例模式,从字面意思上理解,“单例”,即只有唯一一个实例,通常情况下,定义一个类,然后通过new  ClassName()方式来产生具体对象,然而这样,破坏了一个类只有一个实例,怎么处理该问题呢?将类的具体化放在类的构造函数来完成;

    (2)如上方式解决了单例问题,然而,外界如何才能访问该类?很简单,该类提供一个全局的访问点即可;

    (3)根据以上(1),(2)步骤的划分,单例模式有2个很明显特点:a.类只有一个实例 b.类必须提供全局唯一的可被访问点;

    4   参考文献

    【01】《大话设计模式》(中文版),《design patterns:elements of reusable object-oriented software》(英文版)

    【02】《设计模式》(可复用面向对象软件的基础)(中文版),《Design Patterns Elements of Reusable Object-Oriented Software》(英文版)

    【03】《Head First设计模式》(中文版), 《Head First Design Patterns》(英文版)

    【04】《C#设计模式》(中文版),《C# Design Patterns:A Tutorial》(英文版)

    【05】《Java企业设计模式》(中文版),《Java Enterprise Design Patterns》(英文版)

    【06】 《UML和模式应用》(面向对象分析与设计导论)(中文版), 《Applying UML and Patterns:An Introduction to Object-Oriented Analysis and Design》(英文版)

    【07】 《设计模式解析》(中文版),《Design Patterns Explained:A New Perspective on Object-Oriented Design》

    【08】 《.NET 设计规范--.NET约定、惯用法与模式》(中文版),《Framework Design Guidelines : Conventions, Idioms, and Patterns for Reusable .NET Libraries》(英文版)

    【09】 《重构与模式》(中文版),《Refactoring to Patterns》(英文版)

    【10】 《设计模式解析》(中文版),《Design Patterns Explained:A New Perspective on Object-Oriented Design ,Second Edition》(英文版)

    【11】 《深入浅出设计模式》(中文版),(C#/Java版)

    【12】 《多线程与并发处理》

    【13】 《企业应用架构模式》 (中文版),《Patterns of Enterprise Application Architecture》(英文版)

    6   版权

    • 感谢您的阅读,若有不足之处,欢迎指教,共同学习、共同进步。
    • 博主网址:http://www.cnblogs.com/wangjiming/。
    • 极少部分文章利用读书、参考、引用、抄袭、复制和粘贴等多种方式整合而成的,大部分为原创。
    • 如您喜欢,麻烦推荐一下;如您有新想法,欢迎提出,邮箱:2016177728@qq.com。
    • 可以转载该博客,但必须著名博客来源。
  • 相关阅读:
    linux下测试web访问及网络相关的命令
    linux下的数据备份工具rsync讲解
    Centos安装PHP PS:LAMP环境时,为少出错误,先安装一下编译环境
    Centos6.6安装MySQL5.6.24
    Centos6.6安装apache2.4
    bash_profile和bashrc区别
    Centos安装 Apache2.4提示 APR not found的解决办法
    ERROR 1010 (HY000): Error dropping database (can't rmdir './test/', errno: 17)
    MySQL ibdata1文件迁移
    Linux启动/停止/重启Mysql数据库的方法
  • 原文地址:https://www.cnblogs.com/wangjiming/p/6105239.html
Copyright © 2011-2022 走看看