我是否正确实现了基于泛型的 Java 工厂?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6191710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Am I implementing a generics-based Java factory correctly?
提问by Andrew
I don't believe I am implementing the factory pattern correctly because the Application
class' createDocument
method accepts any class type, not just subclasses of Document
.
我不相信我正确地实现了工厂模式,因为Application
类的createDocument
方法接受任何类类型,而不仅仅是Document
.
In other words, is there a way I can restrict the createDocument
method to only accept subclasses of Document
?
换句话说,有没有办法限制createDocument
方法只接受 的子类Document
?
Document.java
package com.example.factory; public abstract class Document { public Document() { System.out.println("New Document instance created: " + this.toString()); } }
DrawingDocument.java
package com.example.factory public class DrawingDocument extends Document { public DrawingDocument() { System.out.println("New DrawingDocument instance created: " this.toString()); } }
Application.java
package com.example.factory; public class Application { public <T> T createDocument(Class<T> documentClass) { try { return documentClass.newInstance(); } catch (InstantiationException e) { throw new IllegalArgumentException(e); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } }; }
Main.java
package com.example.factory; public static void main(String[] args) { Application application = new Application(); application.createDocument(DrawingDocument.class); }
文档.java
package com.example.factory; public abstract class Document { public Document() { System.out.println("New Document instance created: " + this.toString()); } }
绘图文档.java
package com.example.factory public class DrawingDocument extends Document { public DrawingDocument() { System.out.println("New DrawingDocument instance created: " this.toString()); } }
应用程序.java
package com.example.factory; public class Application { public <T> T createDocument(Class<T> documentClass) { try { return documentClass.newInstance(); } catch (InstantiationException e) { throw new IllegalArgumentException(e); } catch (IllegalAccessException e) { throw new IllegalArgumentException(e); } }; }
主程序
package com.example.factory; public static void main(String[] args) { Application application = new Application(); application.createDocument(DrawingDocument.class); }
回答by Yet Another Geek
You should boundyour generic so that it is only using T's that inherit Document. example:
您应该绑定您的泛型,以便它仅使用继承 Document 的 T。例子:
public class Application {
//Add extends Document after T
public static <T extends Document> T createDocument(Class<T> documentClass) throws InstantiationException, IllegalAccessException {
return documentClass.newInstance();
};
}
回答by Roland Illig
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.
例如,faxtory 方法应该将 aClass<? extends Document>
作为其参数,这样就不能要求它创建一个String
。
[update:] Code sample:
[更新:] 代码示例:
public Document createDocument(Class<? extends Document> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException e) {
throw new IllegalArgumentException(e);
}
}
回答by Grooveek
Where is the restriction to Document type in the factory ? Try
工厂对文件类型的限制在哪里?尝试
public <T extends Document> T createDocument(Class<T> documentClass) throws InstantiationException, IllegalAccessException {
return documentClass.newInstance();
};