单例模式的Java泛型实现方式
import java.util.HashMap; import java.util.Map; /** * Created by zhao.wu on 2016/11/18. */ public class SingleTonParent{ private static Map<Class<? extends SingleTonParent>, SingleTonParent> INSTANCES_MAP = new HashMap<>(); public synchronized static <E extends SingleTonParent> SingleTonParent getInstance(Class<E> instanceClass) throws Exception { if(INSTANCES_MAP.containsKey(instanceClass)){ return INSTANCES_MAP.get(instanceClass); } else { E instance = instanceClass.newInstance(); INSTANCES_MAP.put(instanceClass, instance); return instance; } } protected SingleTonParent(){ } }
继承泛型父类
public class Test extends SingleTonParent{
}
- 1
- 2
调用方法
Test test = (Test)SingleTonParent.getInstance(Test.class);