码迷,mamicode.com
首页 > 其他好文 > 详细

singleton pattern

时间:2014-09-17 20:25:02      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   div   sp   log   on   c   ad   

1 normal mode

class Singleton
{
    private Singleton(){};

    Singleton singleton;
    static Singleton getInstance() {
    if(singleton == null)
      singleton = new Singleton(); 
   return singleton;   
   }
}

not thread safe.

Thread safe version:

class Singleton
{
    static private Singleton(){};

    Singleton singleton;
    static Singleton getInstance() {
    if(singleton == null)
      singleton = new Singleton(); 
   return singleton;   
   }
}

  

 

singleton pattern

标签:blog   io   ar   div   sp   log   on   c   ad   

原文地址:http://www.cnblogs.com/williamwood/p/3977925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!