Am I implementing a generics-based Java factory correctly?

后端 未结 3 1358
青春惊慌失措
青春惊慌失措 2021-02-05 14:40

I don\'t believe I am implementing the factory pattern correctly because the Application class\' createDocument method accepts any class type, not just

3条回答
  •  [愿得一人]
    2021-02-05 15:09

    Where is the restriction to Document type in the factory ? Try

    public  T createDocument(Class documentClass) throws InstantiationException, IllegalAccessException {
        return documentClass.newInstance();
    };
    

提交回复
热议问题