欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java 静态工厂,

来源: javaer 分享于  点击 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(); }     } }

相关文章

    暂无相关文章
相关栏目:

用户点评