i m just learning java, and i meet some problems. Here we have simple factory pattern:
public class SomeFactory {
...
public static void registerProduct(String name, Class<? extends IProduct > f)
}
public SomeProduct implements IProduct {
static {
SomeFactory.register("some product", SomeProduct.class);
}
...
}
All products should register themselves at factory.
But before using this code, all Products classes should be loaded.
I can put Class.forName()
somewhere, for example in main function.
But i want to avoid such sort of manual classes loading. I want just add new IProduct
implementations, without updating other parts(such as SomeFactory
or Main
methods, etc.).
But i wonder, is it possible to automatically load some classes(marked with annotation, for example)?
P.S 我想指出,在运行时间,所有<代码>不会增加其他班级。 汇编之前,先了解《电子格式编码>实施情况。
UPD#1
Thank for your answering!
But is it possible to make auto-generated property-file with IProduct
instances?
I mean is it possible to make some build-time script(for maven for example) that generates property-file or loader code? Are there such solutions or frameworks?
UPD#2 I finished with using Reflections library that provides run-time information, by scanning classpath at startup.