1.4 Cache Singleton

這邊我就做了一個簡單的快取單例, 直接用程式說明:

package idv.design.pattern.singleton.cache;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Carl Lu
 */
public class Singleton {

    private static final String DEFAULT_KEY = "One";

    private static Map<String, Singleton> map = new HashMap<String, Singleton>();

    private Singleton() {

    }

    public static Singleton getInstance() {
        Singleton instance = (Singleton)map.get(DEFAULT_KEY);
        if(instance == null) {
            instance = new Singleton();
            map.put(DEFAULT_KEY, instance);
        }
        return instance;
    }

}

原始碼點我

好吧, 我想這個例子有點雞肋, 但這是為了後面鋪梗.

Last updated