设计模式-----Singleton

来源:互联网 发布:final cut pro mac版 编辑:程序博客网 时间:2024/06/14 15:29

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.

Have you met this question in a real interview? Yes
Example
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     */          private Solution(){     }    private static  volatile Solution instance = new Solution();    public static Solution getInstance() {        // write your code here         return instance ;    }};
0 0
原创粉丝点击