public abstract class Product{ public void method1(){ ... } public abstract void method2(); } public class ConcreteProduct1 extends Product{ public void method2(){ .... } } public class ConcreteProduct2 extends Product{ public void method2(){ .... } } public abstract class Creator{ public abstract < T extends Product> T createProduct(Class<T> c); } public class ConcreteCreator extends Creator{ public < T extends Product> T createProduct(Class<T> c){ Product product=null; try{ product=(Product)Class.forName(c.getName()).newInstance(); }catch{ .... } return (T)product; } } public class Client{ public static void main(String[] args){ Creator creator=new ConcreteCreator(); Product product=creator.createProduct(ConcreteProduct1.class); } }
原文:http://www.cnblogs.com/liaojie970/p/5459412.html