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;

             }

    }

     

  • 相关阅读:
    IE表单拦截器
    使用网站作为WCF服务宿主的几种做法
    Javascript执行效率小结
    Ajax无刷新技术实现省市县三级联动下拉菜单Asp.Net
    序列化DataContractSerializer
    变化多端的列表
    腾讯微信
    Mac OS 和 iOS 操作系统架构
    程序员远离你的细节思维
    ObjectiveC概述
  • 原文地址:https://www.cnblogs.com/gxpblogs/p/3068001.html
Copyright © 2011-2022 走看看