zoukankan      html  css  js  c++  java
  • 单例模式

    http://baike.baidu.com/view/1859857.htm
    单例模式一般有三种形式

    //1.饿汉式

    public class Singleton1 {

     

             private static Singleton1 instance = new Singleton1();

     

             private Singleton1() {

             }

     

             static Singleton1 getInstance() {

                       return instance;

             }

     

    }


    *************************************************************************************************************************************************************************

    //2.懒汉式

    public class Singleton2 {

     

             private static Singleton2 instance = null;

     

             private Singleton2() {

             }

     

             static Singleton2 getInstance() {

                       if (instance == null)

                                instance = new Singleton2();

                       return instance;

             }

    }

    *************************************************************************************************************************************************************************


    //
    双重锁的形式。

    public class Singleton3 {

             private static Singleton3 instance = null;

             private Singleton3(){

                       //do something

             }

             public static Singleton3 getInstance(){

                       if(instance==null){

                                synchronized(Singleton3.class){

                                         if(null == instance){ 

                                                   instance = new Singleton3();

                                         }

                                }

                       }

                       return instance;

             }

    }

     

  • 相关阅读:
    Qt 打开UI是提示Runtime Error! 。。。 然后奔溃
    Qt exe和动态 库获取运行所需库
    区分EXE或者动态库dll是32位或者64位方法
    QFile 读2进制文件
    MFC 动态库编译错误
    Qt 编译错误
    Qt QNetworkProxy类帮助翻译
    Qt QHttpMultiPart类帮助翻译
    Qt QNetworkCookie帮助翻译
    Qt QHttpPart翻译
  • 原文地址:https://www.cnblogs.com/gxpblogs/p/3068001.html
Copyright © 2011-2022 走看看