标签:bsp on() instance single pac 检查 get amp return
1.) Java版本
1 package com.tone.test; 2 3 public class Singleton 4 { 5 // 饿汉模式 6 // private static Singleton singleton = new Singleton(); 7 // 8 // private Singleton() 9 // { 10 // 11 // } 12 // 13 // public static Singleton getSingleton() 14 // { 15 // return singleton; 16 // } 17 18 // 懒汉模式: 双重检查锁定 19 private static Singleton singleton = null; 20 21 private Singleton() 22 { 23 24 } 25 26 public static Singleton getSingleton() 27 { 28 if (null == singleton) 29 { 30 synchronized (Singleton.class) 31 { 32 if (null == singleton) 33 { 34 singleton = new Singleton(); 35 } 36 } 37 } 38 39 return singleton; 40 } 41 }
2.) C++版本
1 class Singleton 2 { 3 public: 4 static Singleton & getInstance() 5 { 6 static Singleton instance; 7 return instance; 8 } 9 10 private: 11 Singleton() 12 { 13 14 } 15 16 ~Singleton() 17 { 18 19 } 20 21 Singleton(const Singleton&) 22 { 23 24 } 25 26 Singleton& operator=(const Singleton&) 27 { 28 29 } 30 };
标签:bsp on() instance single pac 检查 get amp return
原文地址:http://www.cnblogs.com/itpoorman/p/6811208.html