Java generics vs dynamically loads a class using Class.forName ()

Suppose I make a class called "Government." The government has members such as officers, ministers, departments, etc. For each of these members I create an interface, and any particular government defines them as it sees fit.

The main method in the government class is called Serve(Request req) . Suppose the request speed is very high (1000+ requests per second).

To create a government, I can:
1) Use Java generics to write Government<Class Minister, Class Officer, ...> , and any specific government implementation should create its own government object in Java code, and main() - an expanded bank.

2) Have a configuration file that indicates the names of the classes of officers, ministers, etc., and whenever Serve() is called, it uses Class.forName() and Class.newInstance() to create a class object. Any new government just needs to write classes for its members and a configuration file. For all governments, there is one main() .

From a purely official point of view - which is better and why? My main concerns:

a) does forName() do an expensive search every time? Suppose there is a very large universe of classes.

b) Will we skip compiler optimization that can be done in case 1, but not in case 2 in dynamic classes?

+4
source share
3 answers

As long as you reuse your government facility, there is no difference at runtime. The difference is only in the time the object was created.

1 and 2 differ in concept - 1 ist hardwired, and 2 is dynamic (you can even use DI filters such as spring, guice or pico - basically you suggested writing your own)

As for forName (), performance is on the class loader (as well as on the container). Most of them will cache the name resolution results, search on the map - but I can’t speak for everyone

As for optimizations - there are compiler optimizations, as well as aggressive runtime optimizations from JIT compilers - they are more important.

+2
source

I do not understand. These two options are not an alternative; they are pretty orthogonal: Generics is a compile-time construct. It is erased and not translated to anything at runtime. On the other hand, loading classes by calling forName is a runtime. One does not affect the other.

If you use generics, this means that you do not need the class object at runtime, since in generics you do not have access to the class object unless you pass it explicitly. If you do not need a class object at runtime, this means that you do not need to load it with forName , so this does not match forName . If you pass the class object explicitly, it means that you already have the class object and do not need to load it, which is also incompatible with forName .

0
source

Your description like this reads to me like this: "I want to use dependency injection, should I roll back my own?"

Take a look at Spring (or Google's Guice). I assume Spring.

Create interfaces for files and configure which implementation to use for each in Spring.

0
source

Source: https://habr.com/ru/post/1386513/


All Articles