LintCode-单例实现

单例模式:

对于任何时刻,如果某个类只存在且最多存在一个具体的实例;所以单例模式需要具备几个条件:

1、自己对象的变量必须私有;

2、构造方法必须私有,不能从外部调用;

3、实现线程锁;

 1 class Solution {
 2     /**
 3      * @return: The same instance of this class every time
 4      */
 5      private static Solution s = null ;
 6      private Solution(){};
 7     public static Solution getInstance() {
 8         // write your code here
 9         if(s==null){
10             synchronized(Solution.class){
11                 s= new Solution();
12             }
13         }
14         return s ;
15     }
16 }
原文地址:https://www.cnblogs.com/crazytrip/p/7338319.html