lintcode-easy-Singleton

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;
    }
};

 

posted @ 2016-03-07 07:11  哥布林工程师  阅读(129)  评论(0编辑  收藏  举报