[Lintcode]Singleton
2016-04-11 21:14
246 查看
Singleton is a most widely used design pattern. If a class has and only has one instance at every moment, we call this design as singleton. For example, for class Mouse (not a animal mouse), we should design it in singleton.
You job is to implement a
the same instance of this class every time you call this method.
new时要注意防止在并发情况下被new多次。
You job is to implement a
getInstancemethod for given class, return
the same instance of this class every time you call this method.
new时要注意防止在并发情况下被new多次。
class Solution { /** * @return: The same instance of this class every time */ private static volatile Solution so = null; private Solution() {} public static Solution getInstance() { if(so == null){ synchronized (Solution.class) so = new Solution(); } return so; } };
相关文章推荐
- LintCode Majority Number iii
- LintCode-最大数
- Subtree
- LintCode --number-of-airplanes-in-the-sky(数飞机)
- LintCode --invert-binary-tree(翻转二叉树)
- LintCode --find-the-missing-number(寻找缺失的数)
- LintCode--best-time-to-buy-and-sell-stock(买卖股票的最佳时机)
- LintCode--best-time-to-buy-and-sell-stock-ii(买卖股票的最佳时机 II)
- 中位数 21% 通过 给定一个未排序的整数数组,找到其中位数。 中位数是排序后数组的中间值,如果数组的个数是偶数个,则返回排序后数组的第N/2个数
- LintCode 翻转二叉树
- LintCode 寻找缺失的数
- LintCode 更新二进制位
- Lintcode 二叉树的后序遍历
- Lintcode 二叉树中序遍历
- Lintcode 二叉树前序遍历
- Lintcode 尾部零的个数
- Lintcode 比较字符串
- Lintcode 主元素
- LintCode 最小调整代价
- LintCode 木材加工