java 静态工厂,
分享于 点击 38503 次 点评:233
java 静态工厂,
public abstract class Foo {
//继承了Foo的类的集合private static Map implementations = null;
private static synchronized void initMapIfNecessary() { if (null == implementations) { implementations = new HashMap(); } } public static Foo getInstance(String key) { initMaoIfNecessary(); Class c = (Class) implementations.get(key); if(c == null) return new DefaultFoo(); try { return (Foo) c.newInstance(); } catch(Exception e) { return new DefaultFoo(); } } }相关文章
- 暂无相关文章
用户点评