JAVA设计模式之单例模式

LYKblogdream 2017-12-01 原文

JAVA设计模式之单例模式

(只适合单线程)
public class Singleton{
  private Singleton instance=null;
  private Singleton(){
  }
  public static Singleton getSingleton(){
    if(instance == null){
    instance = new Single();
    }
      return instance;
  }
}
(懒汉式单例 ->加了同步锁,适用于多线程 ---->   从而产生的问题是:
耗费时间,因为获取锁的过程很耗时)
public class Singleton{
  private Singleton instance=null;
  private Singleton(){
  }
  public static synchronized Singleton getSingleton(){
    if(instance == null){
      instance = new Single();
    }
    return instance;
  }
}

(懒汉式的优化-->只有当instance == null 是才会加同步锁,去创建实例)
public class Singleton{
  private Singleton instance=null;
  private Singleton(){
  }
  public static Singleton getSingleton(){
    if(instance == null){
      synchronized(Singleton.class){
        if(instance == null){
          instance = new Single();
        }
      }
    }
    return instance;
  }
}

(饿汉式:建议适用-->使用了静态属性-在类加载的时候就被创建,且只被创建一次)
public class Singleton{
  private static Singleton instance = new Singleton();
  private Singleton(){
  }
  public static Singleton getInsance(){
    return instance;
  }
}

(静态内部类:不怎么理解)
public class Singleton{
  private Singleton(){
  }
  private static class SingletonHolder{
    private final static instance= new Singleton();
  }
  public static Singleton getInsance(){
    return SingletonHolder.instacne;
  }
}

  

(只适合单线程)

public class Singleton{

private Singleton instance=null;

private Singleton(){

}

public static Singleton getSingleton(){

if(instance == null){

instance = new Single();

}

return instance;

}

}

(懒汉式单例 ->加了同步锁,适用于多线程 —->   从而产生的问题是:

耗费时间,因为获取锁的过程很耗时)

public class Singleton{

private Singleton instance=null;

private Singleton(){

}

public static synchronized Singleton getSingleton(){

if(instance == null){

instance = new Single();

}

return instance;

}

}

 

(懒汉式的优化–>只有当instance == null 是才会加同步锁,去创建实例)

public class Singleton{

private Singleton instance=null;

private Singleton(){

}

public static Singleton getSingleton(){

if(instance == null){

synchronized(Singleton.class){

if(instance == null){

instance = new Single();

}

}

}

return instance;

}

}

 

(饿汉式:建议适用–>使用了静态属性在类加载的时候就被创建,且只被创建一次)

public class Singleton{

private static Singleton instance = new Singleton();

private Singleton(){

}

public static Singleton getInsance(){

return instance;

}

}

 

(静态内部类:不怎么理解)

public class Singleton{

private Singleton(){

}

private static class SingletonHolder{

private final static instance= new Singleton();

}

public static Singleton getInsance(){

return SingletonHolder.instacne;

}

}

发表于 2017-12-01 10:58 我叫李元霸 阅读() 评论() 编辑 收藏

 

版权声明:本文为LYKblogdream原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:http://www.cnblogs.com/LYKblogdream/p/7940919.html

JAVA设计模式之单例模式的更多相关文章

  1. 单例模式

    首先来明确一个问题,那就是在某些情况下,有些对象,我们只需要一个就可以了, 比如,一台计算机上可以连好几个打印 […]...

  2. Java设计模式——工厂模式

    Java设计模式——工厂模式 工厂模式(Factory Pattern)属于创建型模式,它提供了一种创建对象的 […]...

  3. 单例模式相关知识

    单例模式 概念:是一种常用的软件设计模式。在它的核心结构中只包含一个被称为单例的特殊类 作用:保证一个类只有一 […]...

  4. java设计模式—–14、桥接模式

      Bridge 模式又叫做桥接模式,是构造型的设计模式之一。Bridge模式基于类的最小设计原则,通过使用封 […]...

  5. java设计模式–工厂模式

    java设计模式–工厂模式 工厂模式要点:   —简单工厂模式(静态工厂模式)     […]...

  6. 第三梦 单例模式

    初识单例 单例模式,算是我们代码中经常遇见的设计模式之一了。当然我们也上手很快,但是其中的坑也不少,不好好研究 […]...

  7. 设计模式之单例模式

    设计模式之单例模式 1、什么是单例模式? 单例模式就是一个类只会有一个实例。 2、单例模式实现方式 2.1 懒 […]...

  8. JAVA设计模式之模板方法

    1.模板方法的介绍   模板方法模式是基于继承的设计模式,它定义了一个算法的步骤,并允许子类别为一个或多个步骤 […]...

随机推荐

  1. static 关键字有什么作用

    今天主要学习下Java语言中的static关键字。 static是Java50个关键字之一。static关键字 […]...

  2. 知识图谱系列—知识图谱

    【知识图谱之一】 【知识图谱】知识图谱概论 【知识图谱】知识表示与知识建模 【知识图谱】知识抽取与挖掘(I) […]...

  3. 乘风破浪,遇见JavaScript框架之NodeJS,开源且跨平台的服务器端JavaScript运行时环境,WSL和Windows环境安装并部署到Azure初体验

    什么是NodeJShttps://nodejs.orgNode.js是一种开源且跨平台的服务器端JavaScript运行时环境,以Chrome的V8 JavaScript引擎为基础,最初由Ryan Dahl创作并在2009年发布。Nod...

  4. 微信小程序的登陆流程详解 – baraka

    微信小程序的登陆流程详解 由于小程序的登陆和登陆状态维护流程比较复杂,需要客户端和服务器的数次交互以及服务器端 […]...

  5. MGR实现分析 – 成员管理与故障恢复实现 – 张冲andy

    MGR实现分析 – 成员管理与故障恢复实现 MySQL Group Replication(MGR […]...

  6. Fanuc Cnc 数控系统,PC端下发NC程序到CNC端,现场测试通过。 – 有证程序员

    Fanuc Cnc 数控系统,PC端下发NC程序到CNC端,现场测试通过。 1.这几天把FANUC 数据采集( […]...

  7. linux系统操作mysql常用指令 – 何建新

    linux系统操作mysql常用指令 1.终端启动MySQL:/etc/init.d/mysql start; […]...

  8. 给大家分享一个小程序—2048

    微信公众号:CodeId有什么建议可以到公众号里进行留言。 很高兴又和大家见面了,最近写了个小游戏——2048 […]...

展开目录

目录导航