[英]How to use Factory from apache.commons?
What are the uses of the java code in: org.apache.commons.collections15.Factory
java代码在
org.apache.commons.collections15.Factory
中的用途是什么
Factory<Integer>
, Factory<String>
in the constructor of the BarabasiAlbertGenerator in the Java Jung graph package? Factory<Integer>
和Factory<String>
? This is the code I have, and it only outputs a single vertex. 这是我的代码,它仅输出一个顶点。
Factory<Graph<String, Integer>> graphFactory = SparseGraph.getFactory();
Integer[] ints = {1};
String[] strs = {"12"};
Class[] typesStr = {String.class};
Class[] typesInt = {int.class};
Factory<String> vertexFactory = InstantiateFactory.getInstance(String.class, typesStr, strs);
Factory<Integer> edgeFactory = InstantiateFactory.getInstance(Integer.class, typesInt, ints);
HashSet<String> seedVertices = new HashSet();
for(int i = 0; i < 10; i++)
{
seedVertices.add("v"+i);
}
BarabasiAlbertGenerator<String, Integer> barabasiGen = new
BarabasiAlbertGenerator<String,Integer>(graphFactory, vertexFactory,
edgeFactory, seedVertices.size(), 1, seedVertices);
Graph g = barabasiGen.create();
I think my issue has something to do with my vertexFactory and edgeFactory. 我认为我的问题与我的vertexFactory和edgeFactory有关。 To me it seems like my vertexFactory can only create vertices with the value 12, and my edgeFactory can only create edges with the value 1. Therefore, the graph will only have 1 vertice with value 12. Is this reasoning accurate?
在我看来,我的vertexFactory只能创建值为12的顶点,而我的edgeFactory只能创建值为1的边。因此,该图形将只有1个值为12的顶点。这是否正确?
You are making this much, much too complicated. 您正在做的事情太复杂了。
Factory is just an interface for classes that generate objects. Factory只是用于生成对象的类的接口。 It is trivial to implement.
实施起来很简单。
You don't need InstantiationFactory. 您不需要InstantiationFactory。 Just write your own.
只需自己编写即可。 For example:
例如:
Factory<Integer> vertexFactory =
new Factory<Integer>() {
int count;
public Integer create() {
return count++;
}};
Successive calls to vertexFactory.create()
generate a series of Integer
objects in increasing order, starting with 0. 连续调用
vertexFactory.create()
以从0开始的vertexFactory.create()
生成一系列Integer
对象。
The specific nature of the Factory that you want will depend on what properties (if any) you want the vertex objects to have, but you may not actually care. 您想要的Factory的特定性质将取决于您希望顶点对象具有哪些属性(如果有),但是您实际上可能并不在意。 If you do, and you have (say) a
List
of objects that you want to use for vertices, then your Factory
instance can use that list. 如果这样做,并且您拥有(例如)要用于顶点的对象
List
,则Factory
实例可以使用该列表。
Any of the JUNG examples that generate graphs ad hoc, or that use graph generators (rather than a static saved graph) will use Factory
instances. 任何临时生成图或使用图生成器(而不是静态保存的图)的JUNG示例都将使用
Factory
实例。 They're everywhere. 他们无处不在。
From the looks of it (ie the Javadoc ) it is an interface that defines a create
method that creates a new instance: 从它的外观(即Javadoc )来看,它是一个接口,它定义了一个
create
新实例的create
方法:
java.lang.Object create()
Create a new object.
创建一个新对象。
Returns: a new object
返回:一个新对象
How do I use this to instantiate objects of type:
Factory<Integer>
,Factory<String>
?如何使用它实例化类型为Object的对象:
Factory<Integer>
,Factory<String>
?
Actually, you'd use the Factory<Integer>
to instantiate an Integer (not another Factory). 实际上,您将使用
Factory<Integer>
实例化一个Integer(而不是另一个Factory)。
For example 例如
Factory<Integer> factory = ConstantFactory.getInstance(123);
Integer oneTwoThree = factory.create(); // will give you the Integer "123"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.