English 中文(简体)
自动工厂登记
原标题:Automatic factory registration
  • 时间:2011-10-01 08:38:33
  •  标签:
  • java
  • factory

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.

最佳回答
问题回答
  1. Have each product register itself, using a static block like this:

     class MyProduct1{
    
         static{
             SomeFactory.register(MyProduct1.getClass());
         }
         ..
         ..
     }
    
  2. 外部财产档案可以追踪所有产品。

  3. 您的主要方法可以把这一产品清单归为一等。

这样,你就迫切需要制定任何具体产品,而财产档案却在不断变化。 Ah! 是否增加安全登记也将是一个加点。

注:我只是提出一个想法,我发明试图自己:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签