首页 > 其他 > 详细

lintcode-easy-Singleton

时间:2016-03-07 07:45:41      阅读:166      评论:0      收藏:0      [点我收藏+]

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 getInstance method for given class, return the same instance of this class every time you call this method.

In Java:

A a = A.getInstance();
A b = A.getInstance();

a should equal to b.

 

class Solution {
    /**
     * @return: The same instance of this class every time
     */
    public Solution(){
        
    }
    
    public static Solution instance;
    
    public static Solution getInstance() {
        // write your code here
        if(instance == null){
            instance = new Solution();
            return instance;
        }
        else
            return instance;
    }
};

 

lintcode-easy-Singleton

原文:http://www.cnblogs.com/goblinengineer/p/5249277.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!