日期:2014-05-20 浏览次数:20674 次
public class TestSingle { // 不想在每个类中重复这段代码--------begin private static TestSingle instance = new TestSingle(); public static TestSingle getInstance() { return instance; } private TestSingle () { } // 不想在每个类中重复这段代码--------end }
public class Test { private static Map<String, Object> map = new HashMap<String, Object>(); public static Object getSingleObject(String className) throws Exception{ if(map.get(className)==null){ Object obj = Class.forName(className).newInstance(); map.put(className, obj); return obj; } return map.get(className); } }