2017.9.28
用几种不同的方法实现了单例
class Solution {
/**
* @return: The same instance of this class every time
*/
/************** 1 ******************
private static Solution instance;
private Solution (){}
public static Solution getInstance() {
// write your code here
if (instance == null) {
instance = new Solution();
}
return instance;
}
************** 1 ******************/
/************** 2 ******************
private static Solution instance = new Solution();
private Solution (){}
public static Solution getInstance() {
return instance;
}
************** 2 ******************/
/************** 3 ******************
private static class SolutionTmp {
private static final Solution instance = new Solution();
}
private Solution(){}
public static final Solution getInstance() {
return SolutionTmp.instance;
}
************** 3 ******************/
};