• 单例模式


    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;

             }

    }

     

  • 相关阅读:
    数据库信息 (表名 行数 堆 集群 非聚集)的查询
    jquerygalleryview2.0 漂亮多样化的图片特效(多项自定义)
    枚举 EnumDescription 位运算 权限 sql c#
    vs2010缓存类
    透明遮罩层
    app_offline.htm的作用
    XmlToJSON(c#)
    jquery性能最佳实践
    .net面试问答(大汇总)
    apk反编译
  • 原文地址:https://www.cnblogs.com/gxpblogs/p/3068001.html
Copyright © 2020-2023  润新知