I don\'t believe I am implementing the factory pattern correctly because the Application
class\' createDocument
method accepts any class type, not just
The code looks good. In a real implementation the factory method should not be declared to throw any of the reflection-related exceptions. And you will probably have some different code anyway to create the document.
The faxtory method should take a Class extends Document>
as its parameter, so that one cannot ask it to create a String
, for example.
[update:] Code sample:
public Document createDocument(Class extends Document> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException e) {
throw new IllegalArgumentException(e);
}
}